1 package de.halbekunst.juplo.test;
3 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
4 import org.springframework.beans.factory.config.Scope;
5 import org.springframework.context.support.GenericApplicationContext;
6 import org.springframework.context.support.SimpleThreadScope;
7 import org.springframework.test.context.TestContext;
8 import org.springframework.test.context.support.AbstractTestExecutionListener;
11 * Diese Klasse ermöglicht es, Beans mit dem Scope REQUEST in JUnit-Testfällen
14 * Source: http://stackoverflow.com/questions/2411343/request-scoped-beans-in-spring-testing
18 * @RunWith(SpringJUnit4ClassRunner.class)
19 * @ContextConfiguration(locations = "classpath:spring/TestScopedBeans-context.xml")
20 * @TestExecutionListeners({
21 * WebContextTestExecutionListener.class,
22 * DependencyInjectionTestExecutionListener.class,
23 * DirtiesContextTestExecutionListener.class })
24 * public class TestScopedBeans {
28 public class WebContextTestExecutionListener extends AbstractTestExecutionListener {
30 public void prepareTestInstance(TestContext testContext) throws Exception {
32 if (testContext.getApplicationContext() instanceof GenericApplicationContext) {
33 GenericApplicationContext context = (GenericApplicationContext) testContext.getApplicationContext();
34 ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
35 Scope requestScope = new SimpleThreadScope();
36 beanFactory.registerScope("request", requestScope);
37 Scope sessionScope = new SimpleThreadScope();
38 beanFactory.registerScope("session", sessionScope);