From 9f451210fb5577e0bb42737315028cc3595f977d Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Thu, 13 Oct 2011 00:23:27 +0200 Subject: [PATCH] =?utf8?q?Modul=20"test"=20hinzugef=C3=BCgt,=20das=20Hilfs?= =?utf8?q?klassen=20f=C3=BCr=20JUnit-Tests=20enth=C3=A4lt?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Das Modul enthält zunächst nur eine von stackoverflow.com entlehnte klasse, die es ermöglicht, Beans mit dem Scope "request" in Spring-JUnit- Testsfällen zu verwenden. --- pom.xml | 1 + test/pom.xml | 34 +++++++++++++++ .../test/WebContextTestExecutionListener.java | 41 +++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 test/pom.xml create mode 100644 test/src/main/java/de/halbekunst/juplo/test/WebContextTestExecutionListener.java diff --git a/pom.xml b/pom.xml index 1bdbf17a..b043813c 100644 --- a/pom.xml +++ b/pom.xml @@ -24,6 +24,7 @@ + test cachecontrol utils diff --git a/test/pom.xml b/test/pom.xml new file mode 100644 index 00000000..112ce692 --- /dev/null +++ b/test/pom.xml @@ -0,0 +1,34 @@ + + + + 4.0.0 + + + de.halbekunst + juplo + 1.0.1-SNAPSHOT + + + ${pom.parent.artifactId}-test + Juplo - Test + + + + org.springframework + spring-beans + ${springframework.version} + + + org.springframework + spring-context + ${springframework.version} + + + org.springframework + spring-test + ${springframework.version} + + + + diff --git a/test/src/main/java/de/halbekunst/juplo/test/WebContextTestExecutionListener.java b/test/src/main/java/de/halbekunst/juplo/test/WebContextTestExecutionListener.java new file mode 100644 index 00000000..68985c22 --- /dev/null +++ b/test/src/main/java/de/halbekunst/juplo/test/WebContextTestExecutionListener.java @@ -0,0 +1,41 @@ +package de.halbekunst.juplo.test; + +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.beans.factory.config.Scope; +import org.springframework.context.support.GenericApplicationContext; +import org.springframework.context.support.SimpleThreadScope; +import org.springframework.test.context.TestContext; +import org.springframework.test.context.support.AbstractTestExecutionListener; + +/** + * Diese Klasse ermöglicht es, Beans mit dem Scope REQUEST in JUnit-Testfällen + * zu verwenden. + *

+ * Source: http://stackoverflow.com/questions/2411343/request-scoped-beans-in-spring-testing + *

+ * Anwendung: + * + * @RunWith(SpringJUnit4ClassRunner.class) + * @ContextConfiguration(locations = "classpath:spring/TestScopedBeans-context.xml") + * @TestExecutionListeners({ + * WebContextTestExecutionListener.class, + * DependencyInjectionTestExecutionListener.class, + * DirtiesContextTestExecutionListener.class }) + * public class TestScopedBeans { + * ... + * + */ +public class WebContextTestExecutionListener extends AbstractTestExecutionListener { + @Override + public void prepareTestInstance(TestContext testContext) throws Exception { + + if (testContext.getApplicationContext() instanceof GenericApplicationContext) { + GenericApplicationContext context = (GenericApplicationContext) testContext.getApplicationContext(); + ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); + Scope requestScope = new SimpleThreadScope(); + beanFactory.registerScope("request", requestScope); + Scope sessionScope = new SimpleThreadScope(); + beanFactory.registerScope("session", sessionScope); + } + } +} -- 2.20.1