request scoped beans in spring testing

后端 未结 8 925
自闭症患者
自闭症患者 2020-11-30 00:54

I would like to make use of request scoped beans in my app. I use JUnit4 for testing. If I try to create one in a test like this:

@RunWith(SpringJUnit4Clas         


        
8条回答
  •  青春惊慌失措
    2020-11-30 01:16

    Solution for Spring 3.2 or newer

    Spring starting with version 3.2 provides support for session/request scoped beans for integration testing.

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = TestConfig.class)
    @WebAppConfiguration
    public class SampleTest {
    
        @Autowired WebApplicationContext wac;
    
        @Autowired MockHttpServletRequest request;
    
        @Autowired MockHttpSession session;    
    
        @Autowired MySessionBean mySessionBean;
    
        @Autowired MyRequestBean myRequestBean;
    
        @Test
        public void requestScope() throws Exception {
            assertThat(myRequestBean)
               .isSameAs(request.getAttribute("myRequestBean"));
            assertThat(myRequestBean)
               .isSameAs(wac.getBean("myRequestBean", MyRequestBean.class));
        }
    
        @Test
        public void sessionScope() throws Exception {
            assertThat(mySessionBean)
               .isSameAs(session.getAttribute("mySessionBean"));
            assertThat(mySessionBean)
               .isSameAs(wac.getBean("mySessionBean", MySessionBean.class));
        }
    }
    

    Read more: Request and Session Scoped Beans


    Solution for Spring before 3.2 with listener

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = TestConfig.class)
    @TestExecutionListeners({WebContextTestExecutionListener.class,
            DependencyInjectionTestExecutionListener.class,
            DirtiesContextTestExecutionListener.class})
    public class SampleTest {
        ...
    }
    

    WebContextTestExecutionListener.java

    public  class WebContextTestExecutionListener extends AbstractTestExecutionListener {
        @Override
        public void prepareTestInstance(TestContext testContext) {
            if (testContext.getApplicationContext() instanceof GenericApplicationContext) {
                GenericApplicationContext context = (GenericApplicationContext) testContext.getApplicationContext();
                ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
                beanFactory.registerScope(WebApplicationContext.SCOPE_REQUEST,
                        new SimpleThreadScope());
                beanFactory.registerScope(WebApplicationContext.SCOPE_SESSION,
                        new SimpleThreadScope());
            }
        }
    }
    

    Solution for Spring before 3.2 with custom scopes

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = TestConfig.class, locations = "test-config.xml")
    public class SampleTest {
    
    ...
    
    }
    

    TestConfig.java

    @Configuration
    @ComponentScan(...)
    public class TestConfig {
    
        @Bean
        public CustomScopeConfigurer customScopeConfigurer(){
            CustomScopeConfigurer scopeConfigurer = new CustomScopeConfigurer();
    
            HashMap scopes = new HashMap();
            scopes.put(WebApplicationContext.SCOPE_REQUEST,
                    new SimpleThreadScope());
            scopes.put(WebApplicationContext.SCOPE_SESSION,
                    new SimpleThreadScope());
            scopeConfigurer.setScopes(scopes);
    
            return scopeConfigurer
    
    }
    

    or with xml configuration

    test-config.xml

    
        
            
                
                    
                
            
            
                
                    
                
            
        
    
    

    Source code

    Source code for all presented solutions:

    • https://github.com/mariuszs/spring-test-web

提交回复
热议问题