diff --git a/spring-test/src/main/java/org/springframework/test/context/junit4/AbstractJUnit4SpringContextTests.java b/spring-test/src/main/java/org/springframework/test/context/junit4/AbstractJUnit4SpringContextTests.java
index 555892901ee..05d35d01df0 100644
--- a/spring-test/src/main/java/org/springframework/test/context/junit4/AbstractJUnit4SpringContextTests.java
+++ b/spring-test/src/main/java/org/springframework/test/context/junit4/AbstractJUnit4SpringContextTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2012 the original author or authors.
+ * Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@ import org.springframework.test.context.TestContextManager;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
+import org.springframework.test.context.web.ServletTestExecutionListener;
/**
* Abstract base test class which integrates the Spring TestContext
@@ -45,6 +46,15 @@ import org.springframework.test.context.support.DirtiesContextTestExecutionListe
* the appropriate {@link org.springframework.test.context.TestExecutionListener
* TestExecutionListeners} manually.
*
+ *
The following {@link org.springframework.test.context.TestExecutionListener
+ * TestExecutionListeners} are configured by default:
+ *
+ *
+ * - {@link org.springframework.test.context.web.ServletTestExecutionListener}
+ *
- {@link org.springframework.test.context.support.DependencyInjectionTestExecutionListener}
+ *
- {@link org.springframework.test.context.support.DirtiesContextTestExecutionListener}
+ *
+ *
* Note: this class serves only as a convenience for extension. If you do not
* wish for your test classes to be tied to a Spring-specific class hierarchy,
* you may configure your own custom test classes by using
@@ -57,11 +67,16 @@ import org.springframework.test.context.support.DirtiesContextTestExecutionListe
* @see ContextConfiguration
* @see TestContext
* @see TestContextManager
+ * @see TestExecutionListeners
+ * @see ServletTestExecutionListener
+ * @see DependencyInjectionTestExecutionListener
+ * @see DirtiesContextTestExecutionListener
* @see AbstractTransactionalJUnit4SpringContextTests
* @see org.springframework.test.context.testng.AbstractTestNGSpringContextTests
*/
@RunWith(SpringJUnit4ClassRunner.class)
-@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class })
+@TestExecutionListeners({ ServletTestExecutionListener.class, DependencyInjectionTestExecutionListener.class,
+ DirtiesContextTestExecutionListener.class })
public abstract class AbstractJUnit4SpringContextTests implements ApplicationContextAware {
/**
diff --git a/spring-test/src/main/java/org/springframework/test/context/testng/AbstractTestNGSpringContextTests.java b/spring-test/src/main/java/org/springframework/test/context/testng/AbstractTestNGSpringContextTests.java
index e7964d1eaa6..80c74a5bb52 100644
--- a/spring-test/src/main/java/org/springframework/test/context/testng/AbstractTestNGSpringContextTests.java
+++ b/spring-test/src/main/java/org/springframework/test/context/testng/AbstractTestNGSpringContextTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2012 the original author or authors.
+ * Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@ import org.springframework.test.context.TestContextManager;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
+import org.springframework.test.context.web.ServletTestExecutionListener;
import org.testng.IHookCallBack;
import org.testng.IHookable;
@@ -59,16 +60,30 @@ import org.testng.annotations.BeforeMethod;
* {@code super();}.
*
*
+ *
The following {@link org.springframework.test.context.TestExecutionListener
+ * TestExecutionListeners} are configured by default:
+ *
+ *
+ * - {@link org.springframework.test.context.web.ServletTestExecutionListener}
+ *
- {@link org.springframework.test.context.support.DependencyInjectionTestExecutionListener}
+ *
- {@link org.springframework.test.context.support.DirtiesContextTestExecutionListener}
+ *
+ *
* @author Sam Brannen
* @author Juergen Hoeller
* @since 2.5
+ * @see ContextConfiguration
* @see TestContext
* @see TestContextManager
* @see TestExecutionListeners
+ * @see ServletTestExecutionListener
+ * @see DependencyInjectionTestExecutionListener
+ * @see DirtiesContextTestExecutionListener
* @see AbstractTransactionalTestNGSpringContextTests
* @see org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests
*/
-@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class })
+@TestExecutionListeners({ ServletTestExecutionListener.class, DependencyInjectionTestExecutionListener.class,
+ DirtiesContextTestExecutionListener.class })
public abstract class AbstractTestNGSpringContextTests implements IHookable, ApplicationContextAware {
/** Logger available to subclasses */
diff --git a/spring-test/src/test/java/org/springframework/test/context/testng/web/TestNGSpringContextWebTests.java b/spring-test/src/test/java/org/springframework/test/context/testng/web/TestNGSpringContextWebTests.java
new file mode 100644
index 00000000000..05f398895ed
--- /dev/null
+++ b/spring-test/src/test/java/org/springframework/test/context/testng/web/TestNGSpringContextWebTests.java
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2002-2014 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.test.context.testng.web;
+
+import java.io.File;
+
+import javax.servlet.ServletContext;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+import org.springframework.mock.web.MockHttpSession;
+import org.springframework.mock.web.MockServletContext;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
+import org.springframework.test.context.web.WebAppConfiguration;
+import org.springframework.web.context.ServletContextAware;
+import org.springframework.web.context.WebApplicationContext;
+import org.springframework.web.context.request.ServletWebRequest;
+import org.testng.annotations.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * TestNG-based integration tests that verify support for loading a
+ * {@link WebApplicationContext} when extending {@link AbstractTestNGSpringContextTests}.
+ *
+ * @author Sam Brannen
+ * @since 3.2.7
+ */
+@ContextConfiguration
+@WebAppConfiguration
+public class TestNGSpringContextWebTests extends AbstractTestNGSpringContextTests implements ServletContextAware {
+
+ @Configuration
+ static class Config {
+
+ @Bean
+ public String foo() {
+ return "enigma";
+ }
+ }
+
+
+ protected ServletContext servletContext;
+
+ @Autowired
+ protected WebApplicationContext wac;
+
+ @Autowired
+ protected MockServletContext mockServletContext;
+
+ @Autowired
+ protected MockHttpServletRequest request;
+
+ @Autowired
+ protected MockHttpServletResponse response;
+
+ @Autowired
+ protected MockHttpSession session;
+
+ @Autowired
+ protected ServletWebRequest webRequest;
+
+ @Autowired
+ protected String foo;
+
+
+ @Override
+ public void setServletContext(ServletContext servletContext) {
+ this.servletContext = servletContext;
+ }
+
+ @Test
+ public void basicWacFeatures() throws Exception {
+ assertNotNull("ServletContext should be set in the WAC.", wac.getServletContext());
+
+ assertNotNull("ServletContext should have been set via ServletContextAware.", servletContext);
+
+ assertNotNull("ServletContext should have been autowired from the WAC.", mockServletContext);
+ assertNotNull("MockHttpServletRequest should have been autowired from the WAC.", request);
+ assertNotNull("MockHttpServletResponse should have been autowired from the WAC.", response);
+ assertNotNull("MockHttpSession should have been autowired from the WAC.", session);
+ assertNotNull("ServletWebRequest should have been autowired from the WAC.", webRequest);
+
+ Object rootWac = mockServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
+ assertNotNull("Root WAC must be stored in the ServletContext as: "
+ + WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootWac);
+ assertSame("test WAC and Root WAC in ServletContext must be the same object.", wac, rootWac);
+ assertSame("ServletContext instances must be the same object.", mockServletContext, wac.getServletContext());
+ assertSame("ServletContext in the WAC and in the mock request", mockServletContext, request.getServletContext());
+
+ assertEquals("Getting real path for ServletContext resource.",
+ new File("src/main/webapp/index.jsp").getCanonicalPath(), mockServletContext.getRealPath("index.jsp"));
+
+ }
+
+ @Test
+ public void fooEnigmaAutowired() {
+ assertEquals("enigma", foo);
+ }
+
+}
diff --git a/spring-test/src/test/java/org/springframework/test/context/web/JUnit4SpringContextWebTests.java b/spring-test/src/test/java/org/springframework/test/context/web/JUnit4SpringContextWebTests.java
new file mode 100644
index 00000000000..c1133bdc676
--- /dev/null
+++ b/spring-test/src/test/java/org/springframework/test/context/web/JUnit4SpringContextWebTests.java
@@ -0,0 +1,118 @@
+/*
+ * Copyright 2002-2014 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.test.context.web;
+
+import java.io.File;
+
+import javax.servlet.ServletContext;
+
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+import org.springframework.mock.web.MockHttpSession;
+import org.springframework.mock.web.MockServletContext;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
+import org.springframework.web.context.ServletContextAware;
+import org.springframework.web.context.WebApplicationContext;
+import org.springframework.web.context.request.ServletWebRequest;
+
+import static org.junit.Assert.*;
+
+/**
+ * JUnit-based integration tests that verify support for loading a
+ * {@link WebApplicationContext} when extending {@link AbstractJUnit4SpringContextTests}.
+ *
+ * @author Sam Brannen
+ * @since 3.2.7
+ */
+@ContextConfiguration
+@WebAppConfiguration
+public class JUnit4SpringContextWebTests extends AbstractJUnit4SpringContextTests implements ServletContextAware {
+
+ @Configuration
+ static class Config {
+
+ @Bean
+ public String foo() {
+ return "enigma";
+ }
+ }
+
+
+ protected ServletContext servletContext;
+
+ @Autowired
+ protected WebApplicationContext wac;
+
+ @Autowired
+ protected MockServletContext mockServletContext;
+
+ @Autowired
+ protected MockHttpServletRequest request;
+
+ @Autowired
+ protected MockHttpServletResponse response;
+
+ @Autowired
+ protected MockHttpSession session;
+
+ @Autowired
+ protected ServletWebRequest webRequest;
+
+ @Autowired
+ protected String foo;
+
+
+ @Override
+ public void setServletContext(ServletContext servletContext) {
+ this.servletContext = servletContext;
+ }
+
+ @Test
+ public void basicWacFeatures() throws Exception {
+ assertNotNull("ServletContext should be set in the WAC.", wac.getServletContext());
+
+ assertNotNull("ServletContext should have been set via ServletContextAware.", servletContext);
+
+ assertNotNull("ServletContext should have been autowired from the WAC.", mockServletContext);
+ assertNotNull("MockHttpServletRequest should have been autowired from the WAC.", request);
+ assertNotNull("MockHttpServletResponse should have been autowired from the WAC.", response);
+ assertNotNull("MockHttpSession should have been autowired from the WAC.", session);
+ assertNotNull("ServletWebRequest should have been autowired from the WAC.", webRequest);
+
+ Object rootWac = mockServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
+ assertNotNull("Root WAC must be stored in the ServletContext as: "
+ + WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootWac);
+ assertSame("test WAC and Root WAC in ServletContext must be the same object.", wac, rootWac);
+ assertSame("ServletContext instances must be the same object.", mockServletContext, wac.getServletContext());
+ assertSame("ServletContext in the WAC and in the mock request", mockServletContext, request.getServletContext());
+
+ assertEquals("Getting real path for ServletContext resource.",
+ new File("src/main/webapp/index.jsp").getCanonicalPath(), mockServletContext.getRealPath("index.jsp"));
+
+ }
+
+ @Test
+ public void fooEnigmaAutowired() {
+ assertEquals("enigma", foo);
+ }
+
+}