Announcement Announcement Module
No announcement yet.
struts 2 junit4 - Scope 'request' is not active for the current thread Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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!

    public class TestJunitJobOrderAction extends AbstractStruts2SpringJunit4  {
    	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
    @ContextConfiguration( { 
    public abstract class AbstractStruts2SpringJunit4 extends StrutsTestCase implements ApplicationContextAware {
    	    protected static ApplicationContext applicationContext;
    	    public void setApplicationContext(ApplicationContext applicationContext) {
    	        this.applicationContext = applicationContext;
    	    public void setUp() throws Exception {
    	    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">
                <entry key="session">
                    <bean class=""/>
                <entry key="request">
                    <bean class="org.springframework.web.context.request.RequestScope" />
        <bean id="payPeriodString" class="java.lang.String"
              factory-bean="payPeriodFacade" factory-method="getPayPeriodString"
              lazy-init="true" scope="session">
        <bean id="payPeriodFacade" class="" scope="request">
        	<aop:scoped-proxy/><!-- this is not here in production config -->
    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 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.


    • #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.


      • #4
        anyone have an idea on this?