Modul "test" hinzugefügt, das Hilfsklassen für JUnit-Tests enthält
authorKai Moritz <kai@coolibri.de>
Wed, 12 Oct 2011 22:23:27 +0000 (00:23 +0200)
committerKai Moritz <kai@coolibri.de>
Sat, 28 Jan 2012 12:03:30 +0000 (13:03 +0100)
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
test/pom.xml [new file with mode: 0644]
test/src/main/java/de/halbekunst/juplo/test/WebContextTestExecutionListener.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index 1bdbf17..b043813 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -24,6 +24,7 @@
   </developers>
 
   <modules>
+    <module>test</module>
     <module>cachecontrol</module>
     <module>utils</module>
   </modules>
diff --git a/test/pom.xml b/test/pom.xml
new file mode 100644 (file)
index 0000000..112ce69
--- /dev/null
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>de.halbekunst</groupId>
+    <artifactId>juplo</artifactId>
+    <version>1.0.1-SNAPSHOT</version>
+  </parent>
+
+  <artifactId>${pom.parent.artifactId}-test</artifactId>
+  <name>Juplo - Test</name>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring-beans</artifactId>
+      <version>${springframework.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring-context</artifactId>
+      <version>${springframework.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring-test</artifactId>
+      <version>${springframework.version}</version>
+    </dependency>
+  </dependencies>
+
+</project>
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 (file)
index 0000000..68985c2
--- /dev/null
@@ -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.
+ * <p>
+ * Source: http://stackoverflow.com/questions/2411343/request-scoped-beans-in-spring-testing
+ * <p>
+ * Anwendung:
+ * <code>
+ * @RunWith(SpringJUnit4ClassRunner.class)
+ * @ContextConfiguration(locations = "classpath:spring/TestScopedBeans-context.xml")
+ * @TestExecutionListeners({
+ *      WebContextTestExecutionListener.class,
+ *      DependencyInjectionTestExecutionListener.class,
+ *      DirtiesContextTestExecutionListener.class })
+ * public class TestScopedBeans {
+ * ...
+ * </code>
+ */
+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);
+        }
+    }
+}