Skip to content

Commit 603d79a

Browse files
committed
Include ServletTEL in abstract base test classes
The ServletTestExecutionListener is now prepended to the set of default listeners in AbstractJUnit4SpringContextTests and AbstractTestNGSpringContextTests. Issue: SPR-11340 Backport-Commit: 3370f8b
1 parent fdac4a7 commit 603d79a

File tree

4 files changed

+271
-4
lines changed

4 files changed

+271
-4
lines changed

spring-test/src/main/java/org/springframework/test/context/junit4/AbstractJUnit4SpringContextTests.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@
2929
import org.springframework.test.context.TestExecutionListeners;
3030
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
3131
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
32+
import org.springframework.test.context.web.ServletTestExecutionListener;
3233

3334
/**
3435
* Abstract base test class which integrates the <em>Spring TestContext
@@ -45,6 +46,15 @@
4546
* the appropriate {@link org.springframework.test.context.TestExecutionListener
4647
* TestExecutionListeners} manually.</em>
4748
*
49+
* <p>The following {@link org.springframework.test.context.TestExecutionListener
50+
* TestExecutionListeners} are configured by default:
51+
*
52+
* <ul>
53+
* <li>{@link org.springframework.test.context.web.ServletTestExecutionListener}
54+
* <li>{@link org.springframework.test.context.support.DependencyInjectionTestExecutionListener}
55+
* <li>{@link org.springframework.test.context.support.DirtiesContextTestExecutionListener}
56+
* </ul>
57+
*
4858
* <p>Note: this class serves only as a convenience for extension. If you do not
4959
* wish for your test classes to be tied to a Spring-specific class hierarchy,
5060
* you may configure your own custom test classes by using
@@ -57,11 +67,16 @@
5767
* @see ContextConfiguration
5868
* @see TestContext
5969
* @see TestContextManager
70+
* @see TestExecutionListeners
71+
* @see ServletTestExecutionListener
72+
* @see DependencyInjectionTestExecutionListener
73+
* @see DirtiesContextTestExecutionListener
6074
* @see AbstractTransactionalJUnit4SpringContextTests
6175
* @see org.springframework.test.context.testng.AbstractTestNGSpringContextTests
6276
*/
6377
@RunWith(SpringJUnit4ClassRunner.class)
64-
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class })
78+
@TestExecutionListeners({ ServletTestExecutionListener.class, DependencyInjectionTestExecutionListener.class,
79+
DirtiesContextTestExecutionListener.class })
6580
public abstract class AbstractJUnit4SpringContextTests implements ApplicationContextAware {
6681

6782
/**

spring-test/src/main/java/org/springframework/test/context/testng/AbstractTestNGSpringContextTests.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@
3030
import org.springframework.test.context.TestExecutionListeners;
3131
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
3232
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
33+
import org.springframework.test.context.web.ServletTestExecutionListener;
3334

3435
import org.testng.IHookCallBack;
3536
import org.testng.IHookable;
@@ -59,16 +60,30 @@
5960
* {@code super();}.</li>
6061
* </ul>
6162
*
63+
* <p>The following {@link org.springframework.test.context.TestExecutionListener
64+
* TestExecutionListeners} are configured by default:
65+
*
66+
* <ul>
67+
* <li>{@link org.springframework.test.context.web.ServletTestExecutionListener}
68+
* <li>{@link org.springframework.test.context.support.DependencyInjectionTestExecutionListener}
69+
* <li>{@link org.springframework.test.context.support.DirtiesContextTestExecutionListener}
70+
* </ul>
71+
*
6272
* @author Sam Brannen
6373
* @author Juergen Hoeller
6474
* @since 2.5
75+
* @see ContextConfiguration
6576
* @see TestContext
6677
* @see TestContextManager
6778
* @see TestExecutionListeners
79+
* @see ServletTestExecutionListener
80+
* @see DependencyInjectionTestExecutionListener
81+
* @see DirtiesContextTestExecutionListener
6882
* @see AbstractTransactionalTestNGSpringContextTests
6983
* @see org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests
7084
*/
71-
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class })
85+
@TestExecutionListeners({ ServletTestExecutionListener.class, DependencyInjectionTestExecutionListener.class,
86+
DirtiesContextTestExecutionListener.class })
7287
public abstract class AbstractTestNGSpringContextTests implements IHookable, ApplicationContextAware {
7388

7489
/** Logger available to subclasses */
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright 2002-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.testng.web;
18+
19+
import java.io.File;
20+
21+
import javax.servlet.ServletContext;
22+
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.context.annotation.Bean;
25+
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.mock.web.MockHttpServletRequest;
27+
import org.springframework.mock.web.MockHttpServletResponse;
28+
import org.springframework.mock.web.MockHttpSession;
29+
import org.springframework.mock.web.MockServletContext;
30+
import org.springframework.test.context.ContextConfiguration;
31+
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
32+
import org.springframework.test.context.web.WebAppConfiguration;
33+
import org.springframework.web.context.ServletContextAware;
34+
import org.springframework.web.context.WebApplicationContext;
35+
import org.springframework.web.context.request.ServletWebRequest;
36+
import org.testng.annotations.Test;
37+
38+
import static org.junit.Assert.*;
39+
40+
/**
41+
* TestNG-based integration tests that verify support for loading a
42+
* {@link WebApplicationContext} when extending {@link AbstractTestNGSpringContextTests}.
43+
*
44+
* @author Sam Brannen
45+
* @since 3.2.7
46+
*/
47+
@ContextConfiguration
48+
@WebAppConfiguration
49+
public class TestNGSpringContextWebTests extends AbstractTestNGSpringContextTests implements ServletContextAware {
50+
51+
@Configuration
52+
static class Config {
53+
54+
@Bean
55+
public String foo() {
56+
return "enigma";
57+
}
58+
}
59+
60+
61+
protected ServletContext servletContext;
62+
63+
@Autowired
64+
protected WebApplicationContext wac;
65+
66+
@Autowired
67+
protected MockServletContext mockServletContext;
68+
69+
@Autowired
70+
protected MockHttpServletRequest request;
71+
72+
@Autowired
73+
protected MockHttpServletResponse response;
74+
75+
@Autowired
76+
protected MockHttpSession session;
77+
78+
@Autowired
79+
protected ServletWebRequest webRequest;
80+
81+
@Autowired
82+
protected String foo;
83+
84+
85+
@Override
86+
public void setServletContext(ServletContext servletContext) {
87+
this.servletContext = servletContext;
88+
}
89+
90+
@Test
91+
public void basicWacFeatures() throws Exception {
92+
assertNotNull("ServletContext should be set in the WAC.", wac.getServletContext());
93+
94+
assertNotNull("ServletContext should have been set via ServletContextAware.", servletContext);
95+
96+
assertNotNull("ServletContext should have been autowired from the WAC.", mockServletContext);
97+
assertNotNull("MockHttpServletRequest should have been autowired from the WAC.", request);
98+
assertNotNull("MockHttpServletResponse should have been autowired from the WAC.", response);
99+
assertNotNull("MockHttpSession should have been autowired from the WAC.", session);
100+
assertNotNull("ServletWebRequest should have been autowired from the WAC.", webRequest);
101+
102+
Object rootWac = mockServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
103+
assertNotNull("Root WAC must be stored in the ServletContext as: "
104+
+ WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootWac);
105+
assertSame("test WAC and Root WAC in ServletContext must be the same object.", wac, rootWac);
106+
assertSame("ServletContext instances must be the same object.", mockServletContext, wac.getServletContext());
107+
assertSame("ServletContext in the WAC and in the mock request", mockServletContext, request.getServletContext());
108+
109+
assertEquals("Getting real path for ServletContext resource.",
110+
new File("src/main/webapp/index.jsp").getCanonicalPath(), mockServletContext.getRealPath("index.jsp"));
111+
112+
}
113+
114+
@Test
115+
public void fooEnigmaAutowired() {
116+
assertEquals("enigma", foo);
117+
}
118+
119+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2002-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.web;
18+
19+
import java.io.File;
20+
21+
import javax.servlet.ServletContext;
22+
23+
import org.junit.Test;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.mock.web.MockHttpServletRequest;
28+
import org.springframework.mock.web.MockHttpServletResponse;
29+
import org.springframework.mock.web.MockHttpSession;
30+
import org.springframework.mock.web.MockServletContext;
31+
import org.springframework.test.context.ContextConfiguration;
32+
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
33+
import org.springframework.web.context.ServletContextAware;
34+
import org.springframework.web.context.WebApplicationContext;
35+
import org.springframework.web.context.request.ServletWebRequest;
36+
37+
import static org.junit.Assert.*;
38+
39+
/**
40+
* JUnit-based integration tests that verify support for loading a
41+
* {@link WebApplicationContext} when extending {@link AbstractJUnit4SpringContextTests}.
42+
*
43+
* @author Sam Brannen
44+
* @since 3.2.7
45+
*/
46+
@ContextConfiguration
47+
@WebAppConfiguration
48+
public class JUnit4SpringContextWebTests extends AbstractJUnit4SpringContextTests implements ServletContextAware {
49+
50+
@Configuration
51+
static class Config {
52+
53+
@Bean
54+
public String foo() {
55+
return "enigma";
56+
}
57+
}
58+
59+
60+
protected ServletContext servletContext;
61+
62+
@Autowired
63+
protected WebApplicationContext wac;
64+
65+
@Autowired
66+
protected MockServletContext mockServletContext;
67+
68+
@Autowired
69+
protected MockHttpServletRequest request;
70+
71+
@Autowired
72+
protected MockHttpServletResponse response;
73+
74+
@Autowired
75+
protected MockHttpSession session;
76+
77+
@Autowired
78+
protected ServletWebRequest webRequest;
79+
80+
@Autowired
81+
protected String foo;
82+
83+
84+
@Override
85+
public void setServletContext(ServletContext servletContext) {
86+
this.servletContext = servletContext;
87+
}
88+
89+
@Test
90+
public void basicWacFeatures() throws Exception {
91+
assertNotNull("ServletContext should be set in the WAC.", wac.getServletContext());
92+
93+
assertNotNull("ServletContext should have been set via ServletContextAware.", servletContext);
94+
95+
assertNotNull("ServletContext should have been autowired from the WAC.", mockServletContext);
96+
assertNotNull("MockHttpServletRequest should have been autowired from the WAC.", request);
97+
assertNotNull("MockHttpServletResponse should have been autowired from the WAC.", response);
98+
assertNotNull("MockHttpSession should have been autowired from the WAC.", session);
99+
assertNotNull("ServletWebRequest should have been autowired from the WAC.", webRequest);
100+
101+
Object rootWac = mockServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
102+
assertNotNull("Root WAC must be stored in the ServletContext as: "
103+
+ WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootWac);
104+
assertSame("test WAC and Root WAC in ServletContext must be the same object.", wac, rootWac);
105+
assertSame("ServletContext instances must be the same object.", mockServletContext, wac.getServletContext());
106+
assertSame("ServletContext in the WAC and in the mock request", mockServletContext, request.getServletContext());
107+
108+
assertEquals("Getting real path for ServletContext resource.",
109+
new File("src/main/webapp/index.jsp").getCanonicalPath(), mockServletContext.getRealPath("index.jsp"));
110+
111+
}
112+
113+
@Test
114+
public void fooEnigmaAutowired() {
115+
assertEquals("enigma", foo);
116+
}
117+
118+
}

0 commit comments

Comments
 (0)