Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h1>Solution for Spring 3.2 or newer</h1> <p><strong>Spring starting with version 3.2 <a href="https://jira.spring.io/browse/SPR-4588" rel="noreferrer">provides support for session/request scoped beans for integration testing</a>.</strong></p> <pre><code>@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)); } } </code></pre> <p>Read more: <a href="http://spring.io/blog/2012/11/07/spring-framework-3-2-rc1-new-testing-features/#request-and-session-scoped-beans" rel="noreferrer">Request and Session Scoped Beans</a></p> <hr> <h1>Solution for Spring before 3.2 with listener</h1> <pre><code>@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = TestConfig.class) @TestExecutionListeners({WebContextTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class}) public class SampleTest { ... } </code></pre> <h2><code>WebContextTestExecutionListener.java</code></h2> <pre><code>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()); } } } </code></pre> <hr> <h1>Solution for Spring before 3.2 with custom scopes</h1> <pre><code>@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = TestConfig.class, locations = "test-config.xml") public class SampleTest { ... } </code></pre> <h2><code>TestConfig.java</code></h2> <pre><code>@Configuration @ComponentScan(...) public class TestConfig { @Bean public CustomScopeConfigurer customScopeConfigurer(){ CustomScopeConfigurer scopeConfigurer = new CustomScopeConfigurer(); HashMap&lt;String, Object&gt; scopes = new HashMap&lt;String, Object&gt;(); scopes.put(WebApplicationContext.SCOPE_REQUEST, new SimpleThreadScope()); scopes.put(WebApplicationContext.SCOPE_SESSION, new SimpleThreadScope()); scopeConfigurer.setScopes(scopes); return scopeConfigurer } </code></pre> <p>or with xml configuration </p> <h2><code>test-config.xml</code></h2> <pre><code>&lt;bean class="org.springframework.beans.factory.config.CustomScopeConfigurer"&gt; &lt;property name="scopes"&gt; &lt;map&gt; &lt;entry key="request"&gt; &lt;bean class="org.springframework.context.support.SimpleThreadScope"/&gt; &lt;/entry&gt; &lt;/map&gt; &lt;map&gt; &lt;entry key="session"&gt; &lt;bean class="org.springframework.context.support.SimpleThreadScope"/&gt; &lt;/entry&gt; &lt;/map&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre> <h1>Source code</h1> <p>Source code for all presented solutions:</p> <ul> <li><a href="https://github.com/mariuszs/spring-test-web" rel="noreferrer">https://github.com/mariuszs/spring-test-web</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload