Announcement Announcement Module
Collapse
No announcement yet.
Integration tests with mocked session Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    The Solution of pbdavey works perfectly finte. Thanks.

    One remark, yuo can't autowire beans by method like:

    private BenutzerkontoService BenutzerkontoService;

    @Autowired
    public void setBenutzerkontoService(BenutzerkontoServiceImpl BenutzerkontoService){
    this.BenutzerkontoService = BenutzerkontoService;
    }

    but if you simply use

    @Autowired
    private BenutzerkontoService BenutzerkontoService;

    it works fine.
    Last edited by t.heuer; Jan 23rd, 2009, 09:52 AM.

    Comment


    • #17
      JIRA Issue concerning this

      Hi,

      there'a a JIRA issue concerning integration testing of session scoped beans, see http://jira.springframework.org/browse/SPR-4588

      Stefan

      Comment


      • #18
        I fixed it using this

        In context.xml:

        <bean class="org.springframework.beans.factory.config.Cu stomScopeConfigurer">
        <property name="scopes">
        <map>
        <entry key="session"><bean class="SetupSession"/></entry>
        </map>
        </property>
        </bean>

        <context:component-scan base-package="test" />

        Note the definition of CustomScopeConfigurer bean BEFORE component scan definition.

        The SetupSession class is as follows:

        import org.springframework.beans.factory.InitializingBean ;
        import org.springframework.mock.web.MockHttpServletReques t;
        import org.springframework.mock.web.MockHttpSession;
        import org.springframework.web.context.request.RequestCon textHolder;
        import org.springframework.web.context.request.ServletReq uestAttributes;
        import org.springframework.web.context.request.SessionSco pe;

        public class SetupSession extends SessionScope implements InitializingBean {

        @Override
        public void afterPropertiesSet() throws Exception {
        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpSession session = new MockHttpSession();
        request.setSession(session);
        RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
        }
        }

        Comment


        • #19
          great solution

          Hi,

          The below solution works fine. Thanks a lot.

          Originally posted by pbdavey View Post
          So I spent today messing around with this same stuff and came up with the following solution...

          Start with a basic Spring Test
          Code:
          @RunWith(SpringJUnit4ClassRunner.class)
          @ContextConfiguration(locations=("test-spring-config.xml"))
          @TestExecutionListeners({DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class})
          Change Context Configuration
          Code:
          @ContextConfiguration(locations=("test-spring-config.xml"), loader=TestWebSessionContextLoader.class)
          Add TestWebContextLoader.java (no session yet, but a good base to extend for request, etc) - this is basically a rip-off of the AbstractGenericContextLoader and GenericXmlContextLoader, in fact, this would all be easier if the AbstractGenericContextLoader didn't have loadContext declared as final. However, one important difference is that the context refresh is in customizeContext as it must be refreshed before registerScope is called for the bean factory.
          Code:
          import org.apache.commons.logging.Log;
          import org.apache.commons.logging.LogFactory;
          
          import org.springframework.beans.factory.support.BeanDefinitionReader;
          import org.springframework.beans.factory.support.DefaultListableBeanFactory;
          import org.springframework.context.ConfigurableApplicationContext;
          import org.springframework.context.annotation.AnnotationConfigUtils;
          import org.springframework.context.support.GenericApplicationContext;
          import org.springframework.web.context.support.GenericWebApplicationContext;
          import org.springframework.util.StringUtils;
          import org.springframework.test.context.support.AbstractContextLoader;
          import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
          
          public class TestWebContextLoader extends AbstractContextLoader {
          
          	protected static final Log logger = LogFactory
          			.getLog(TestWebContextLoader.class);
          
          	public final ConfigurableApplicationContext loadContext(
          			final String... locations) throws Exception {
          
          		if (logger.isDebugEnabled()) {
          			logger.debug("Loading ApplicationContext for locations ["
          							+ StringUtils.arrayToCommaDelimitedString(locations)
          							+ "].");
          		}
          
          		GenericWebApplicationContext context = new GenericWebApplicationContext();
          		customizeBeanFactory(context.getDefaultListableBeanFactory());
          		createBeanDefinitionReader(context).loadBeanDefinitions(locations);
          		AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
          		customizeContext(context);
          		
          		context.registerShutdownHook();
          		return context;
          	}
          
          	protected void customizeBeanFactory(
          			final DefaultListableBeanFactory beanFactory) {
          		/* no-op */
          	}
          
          	protected void customizeContext(final GenericWebApplicationContext context) {
                                /* refresh must be called when customizeContext is overriden */
          		context.refresh();
          	}
          
          	protected BeanDefinitionReader createBeanDefinitionReader(final GenericApplicationContext context) {
          		return new XmlBeanDefinitionReader(context);
          	}
          
          	@Override
          	public String getResourceSuffix() {
          		return "-context.xml";
          	}
          }
          Add Session-version of the TestWebContextLoader: TestWebSessionContextLoader.java
          Code:
          import org.springframework.web.context.support.GenericWebApplicationContext;
          
          import org.springframework.web.context.request.RequestContextHolder;
          import org.springframework.web.context.request.ServletRequestAttributes;
          import org.springframework.web.context.request.SessionScope;
          import org.springframework.mock.web.MockHttpServletRequest;
          import org.springframework.mock.web.MockHttpSession;
          import org.springframework.mock.web.MockServletContext;
          
          
          public class TestWebSessionContextLoader extends TestWebContextLoader {
          	
          	@Override
          	protected void customizeContext(final GenericWebApplicationContext context) {
          		MockServletContext servlet = new MockServletContext();
          		context.setServletContext(servlet);
          		
          		MockHttpServletRequest request = new MockHttpServletRequest();
          		MockHttpSession session = new MockHttpSession();
          		request.setSession(session);
          		RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
          	
          		context.refresh();
          		context.getBeanFactory().registerScope("session", new SessionScope());
          	}
          }
          Tada! You now have an ApplicationContext with a mock session. If there's any issues, let me know, but I think it works ok.

          Comment

          Working...
          X