Announcement Announcement Module
Collapse
No announcement yet.
struts 2 junit4 - Scope 'request' is not active for the current thread Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • struts 2 junit4 - Scope 'request' is not active for the current thread

    I am attempting to build an initial junit test for our struts 2 spring 3, hibernate web application. I attempt the run the following test and keep gettting the same error. Im all google'd out and have yet to find an answer. thanks!

    Code:
    public class TestJunitJobOrderAction extends AbstractStruts2SpringJunit4  {
    @Test
    	public void testOrdersAction() throws Exception {
    		ActionProxy proxy = super.getActionProxy("/acct/orders");
    		ActionContext actionContext = proxy.getInvocation().getInvocationContext();
    		String str = (String) applicationContext.getBean("payPeriodString");
    		//.....removed for brevity
    	}
    }
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration( { 
    	"classpath:testContext.xml"
    })
    public abstract class AbstractStruts2SpringJunit4 extends StrutsTestCase implements ApplicationContextAware {
    
    	    protected static ApplicationContext applicationContext;
    
    	    public void setApplicationContext(ApplicationContext applicationContext) {
    	        this.applicationContext = applicationContext;
    	    }
    
    
    
    	    @Before
    	    public void setUp() throws Exception {
    	        super.setUp();
    	    }
    
    	    @Override
    	    protected void setupBeforeInitDispatcher() throws Exception {
    	        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext);
    	    }
    }
    relevant context entries:
    HTML Code:
        <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
            <property name="scopes">
                <map>
                <entry key="session">
                    <bean class="org.springframework.context.support.SimpleThreadScope"/>
                </entry>
                <entry key="request">
                    <bean class="org.springframework.web.context.request.RequestScope" />
                </entry>
                </map>
            </property>
        </bean>
    
        <bean id="payPeriodString" class="java.lang.String"
              factory-bean="payPeriodFacade" factory-method="getPayPeriodString"
              lazy-init="true" scope="session">
        </bean>
    
        <bean id="payPeriodFacade" class="ataaps.biz.facade.PayPeriodFacade" scope="request">
        	<aop:scoped-proxy/><!-- this is not here in production config -->
        </bean>
    error returned:

    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'payPeriodString' defined in class path resource [testContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionSt oreException: Factory method [public java.lang.String ataaps.biz.facade.PayPeriodFacade.getPayPeriodStri ng()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'scopedTarget.payPeriodFacade': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

  • #2
    You can't use 'session' and 'request' scopes outside a WebApplicationContext.
    JUnit Tests execution takes place in a ClasspathXmlApplicationContext, or FileSystemApplicationContext.

    Comment


    • #3
      Mock session and mock request objects are used to simulate them as would normally be provided by the WebApplicationContext, thereby allowing one to test session and request while outside of the container. From my research, this is common and what i have tried to do here. thank you.

      Comment


      • #4
        anyone have an idea on this?

        Comment

        Working...
        X