Skip to content

Commit 3c5a9b4

Browse files
committed
Introduce class-level @DirtiesContext tests for TestNG
Prior to this commit, ClassLevelDirtiesContextTests existed for verifying the expected behavior of @DirtiesContext declared at the class level in conjunction with JUnit and SpringJUnit4ClassRunner. This commit introduces analogous tests for TestNG in the new ClassLevelDirtiesContextTestNGTests class. Furthermore, ContextCacheTestUtils and TrackingTestNGTestListener have been introduced to reduce code duplication across the test suite. Issue: SPR-12918
1 parent d31f3ee commit 3c5a9b4

File tree

7 files changed

+436
-134
lines changed

7 files changed

+436
-134
lines changed
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
/*
2+
* Copyright 2002-2015 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;
18+
19+
import java.util.concurrent.atomic.AtomicInteger;
20+
21+
import org.junit.AfterClass;
22+
import org.junit.BeforeClass;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.junit.runners.JUnit4;
26+
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.context.ApplicationContext;
29+
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.test.annotation.DirtiesContext;
31+
import org.springframework.test.annotation.DirtiesContext.ClassMode;
32+
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
33+
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
34+
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
35+
import org.springframework.test.context.testng.TrackingTestNGTestListener;
36+
37+
import org.testng.TestNG;
38+
39+
import static org.junit.Assert.*;
40+
import static org.springframework.test.context.ContextCacheTestUtils.*;
41+
42+
/**
43+
* JUnit 4 based integration test which verifies correct {@linkplain ContextCache
44+
* application context caching} in conjunction with Spring's TestNG support
45+
* and {@link DirtiesContext @DirtiesContext} at the class level.
46+
*
47+
* <p>This class is a direct copy of {@link ClassLevelDirtiesContextTests},
48+
* modified to verify behavior in conjunction with TestNG.
49+
*
50+
* @author Sam Brannen
51+
* @since 4.2
52+
*/
53+
@RunWith(JUnit4.class)
54+
public class ClassLevelDirtiesContextTestNGTests {
55+
56+
private static final AtomicInteger cacheHits = new AtomicInteger(0);
57+
private static final AtomicInteger cacheMisses = new AtomicInteger(0);
58+
59+
60+
private static final void runTestClassAndAssertStats(Class<?> testClass, int expectedTestCount) {
61+
final int expectedTestFailureCount = 0;
62+
final int expectedTestStartedCount = expectedTestCount;
63+
final int expectedTestFinishedCount = expectedTestCount;
64+
65+
final TrackingTestNGTestListener listener = new TrackingTestNGTestListener();
66+
final TestNG testNG = new TestNG();
67+
testNG.addListener(listener);
68+
testNG.setTestClasses(new Class<?>[] { testClass });
69+
testNG.setVerbose(0);
70+
testNG.run();
71+
72+
assertEquals("Failures for test class [" + testClass + "].", expectedTestFailureCount,
73+
listener.testFailureCount);
74+
assertEquals("Tests started for test class [" + testClass + "].", expectedTestStartedCount,
75+
listener.testStartCount);
76+
assertEquals("Successful tests for test class [" + testClass + "].", expectedTestFinishedCount,
77+
listener.testSuccessCount);
78+
}
79+
80+
@BeforeClass
81+
public static void verifyInitialCacheState() {
82+
ContextCache contextCache = TestContextManager.contextCache;
83+
contextCache.clear();
84+
contextCache.clearStatistics();
85+
cacheHits.set(0);
86+
cacheMisses.set(0);
87+
assertContextCacheStatistics("BeforeClass", 0, cacheHits.get(), cacheMisses.get());
88+
}
89+
90+
@Test
91+
public void verifyDirtiesContextBehavior() throws Exception {
92+
93+
assertBehaviorForCleanTestCase();
94+
95+
runTestClassAndAssertStats(ClassLevelDirtiesContextWithCleanMethodsAndDefaultModeTestCase.class, 1);
96+
assertContextCacheStatistics("after class-level @DirtiesContext with clean test method and default class mode",
97+
0, cacheHits.incrementAndGet(), cacheMisses.get());
98+
assertBehaviorForCleanTestCase();
99+
100+
runTestClassAndAssertStats(InheritedClassLevelDirtiesContextWithCleanMethodsAndDefaultModeTestCase.class, 1);
101+
assertContextCacheStatistics(
102+
"after inherited class-level @DirtiesContext with clean test method and default class mode", 0,
103+
cacheHits.incrementAndGet(), cacheMisses.get());
104+
assertBehaviorForCleanTestCase();
105+
106+
runTestClassAndAssertStats(ClassLevelDirtiesContextWithCleanMethodsAndAfterClassModeTestCase.class, 1);
107+
assertContextCacheStatistics("after class-level @DirtiesContext with clean test method and AFTER_CLASS mode",
108+
0, cacheHits.incrementAndGet(), cacheMisses.get());
109+
assertBehaviorForCleanTestCase();
110+
111+
runTestClassAndAssertStats(InheritedClassLevelDirtiesContextWithCleanMethodsAndAfterClassModeTestCase.class, 1);
112+
assertContextCacheStatistics(
113+
"after inherited class-level @DirtiesContext with clean test method and AFTER_CLASS mode", 0,
114+
cacheHits.incrementAndGet(), cacheMisses.get());
115+
assertBehaviorForCleanTestCase();
116+
117+
runTestClassAndAssertStats(ClassLevelDirtiesContextWithAfterEachTestMethodModeTestCase.class, 3);
118+
assertContextCacheStatistics(
119+
"after class-level @DirtiesContext with clean test method and AFTER_EACH_TEST_METHOD mode", 0,
120+
cacheHits.incrementAndGet(), cacheMisses.addAndGet(2));
121+
assertBehaviorForCleanTestCase();
122+
123+
runTestClassAndAssertStats(InheritedClassLevelDirtiesContextWithAfterEachTestMethodModeTestCase.class, 3);
124+
assertContextCacheStatistics(
125+
"after inherited class-level @DirtiesContext with clean test method and AFTER_EACH_TEST_METHOD mode", 0,
126+
cacheHits.incrementAndGet(), cacheMisses.addAndGet(2));
127+
assertBehaviorForCleanTestCase();
128+
129+
runTestClassAndAssertStats(ClassLevelDirtiesContextWithDirtyMethodsTestCase.class, 1);
130+
assertContextCacheStatistics("after class-level @DirtiesContext with dirty test method", 0,
131+
cacheHits.incrementAndGet(), cacheMisses.get());
132+
runTestClassAndAssertStats(ClassLevelDirtiesContextWithDirtyMethodsTestCase.class, 1);
133+
assertContextCacheStatistics("after class-level @DirtiesContext with dirty test method", 0, cacheHits.get(),
134+
cacheMisses.incrementAndGet());
135+
runTestClassAndAssertStats(ClassLevelDirtiesContextWithDirtyMethodsTestCase.class, 1);
136+
assertContextCacheStatistics("after class-level @DirtiesContext with dirty test method", 0, cacheHits.get(),
137+
cacheMisses.incrementAndGet());
138+
assertBehaviorForCleanTestCase();
139+
140+
runTestClassAndAssertStats(InheritedClassLevelDirtiesContextWithDirtyMethodsTestCase.class, 1);
141+
assertContextCacheStatistics("after inherited class-level @DirtiesContext with dirty test method", 0,
142+
cacheHits.incrementAndGet(), cacheMisses.get());
143+
runTestClassAndAssertStats(InheritedClassLevelDirtiesContextWithDirtyMethodsTestCase.class, 1);
144+
assertContextCacheStatistics("after inherited class-level @DirtiesContext with dirty test method", 0,
145+
cacheHits.get(), cacheMisses.incrementAndGet());
146+
runTestClassAndAssertStats(InheritedClassLevelDirtiesContextWithDirtyMethodsTestCase.class, 1);
147+
assertContextCacheStatistics("after inherited class-level @DirtiesContext with dirty test method", 0,
148+
cacheHits.get(), cacheMisses.incrementAndGet());
149+
assertBehaviorForCleanTestCase();
150+
151+
runTestClassAndAssertStats(ClassLevelDirtiesContextWithCleanMethodsAndAfterClassModeTestCase.class, 1);
152+
assertContextCacheStatistics("after class-level @DirtiesContext with clean test method and AFTER_CLASS mode",
153+
0, cacheHits.incrementAndGet(), cacheMisses.get());
154+
}
155+
156+
private void assertBehaviorForCleanTestCase() {
157+
runTestClassAndAssertStats(CleanTestCase.class, 1);
158+
assertContextCacheStatistics("after clean test class", 1, cacheHits.get(), cacheMisses.incrementAndGet());
159+
}
160+
161+
@AfterClass
162+
public static void verifyFinalCacheState() {
163+
assertContextCacheStatistics("AfterClass", 0, cacheHits.get(), cacheMisses.get());
164+
}
165+
166+
167+
// -------------------------------------------------------------------
168+
169+
@TestExecutionListeners(listeners = { DependencyInjectionTestExecutionListener.class,
170+
DirtiesContextTestExecutionListener.class }, inheritListeners = false)
171+
@ContextConfiguration
172+
public static abstract class BaseTestCase extends AbstractTestNGSpringContextTests {
173+
174+
@Configuration
175+
static class Config {
176+
/* no beans */
177+
}
178+
179+
180+
@Autowired
181+
protected ApplicationContext applicationContext;
182+
183+
184+
protected void assertApplicationContextWasAutowired() {
185+
org.testng.Assert.assertNotNull(this.applicationContext,
186+
"The application context should have been autowired.");
187+
}
188+
}
189+
190+
public static final class CleanTestCase extends BaseTestCase {
191+
192+
@org.testng.annotations.Test
193+
public void verifyContextWasAutowired() {
194+
assertApplicationContextWasAutowired();
195+
}
196+
197+
}
198+
199+
@DirtiesContext
200+
public static class ClassLevelDirtiesContextWithCleanMethodsAndDefaultModeTestCase extends BaseTestCase {
201+
202+
@org.testng.annotations.Test
203+
public void verifyContextWasAutowired() {
204+
assertApplicationContextWasAutowired();
205+
}
206+
}
207+
208+
public static class InheritedClassLevelDirtiesContextWithCleanMethodsAndDefaultModeTestCase extends
209+
ClassLevelDirtiesContextWithCleanMethodsAndDefaultModeTestCase {
210+
}
211+
212+
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
213+
public static class ClassLevelDirtiesContextWithCleanMethodsAndAfterClassModeTestCase extends BaseTestCase {
214+
215+
@org.testng.annotations.Test
216+
public void verifyContextWasAutowired() {
217+
assertApplicationContextWasAutowired();
218+
}
219+
}
220+
221+
public static class InheritedClassLevelDirtiesContextWithCleanMethodsAndAfterClassModeTestCase extends
222+
ClassLevelDirtiesContextWithCleanMethodsAndAfterClassModeTestCase {
223+
}
224+
225+
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
226+
public static class ClassLevelDirtiesContextWithAfterEachTestMethodModeTestCase extends BaseTestCase {
227+
228+
@org.testng.annotations.Test
229+
public void verifyContextWasAutowired1() {
230+
assertApplicationContextWasAutowired();
231+
}
232+
233+
@org.testng.annotations.Test
234+
public void verifyContextWasAutowired2() {
235+
assertApplicationContextWasAutowired();
236+
}
237+
238+
@org.testng.annotations.Test
239+
public void verifyContextWasAutowired3() {
240+
assertApplicationContextWasAutowired();
241+
}
242+
}
243+
244+
public static class InheritedClassLevelDirtiesContextWithAfterEachTestMethodModeTestCase extends
245+
ClassLevelDirtiesContextWithAfterEachTestMethodModeTestCase {
246+
}
247+
248+
@DirtiesContext
249+
public static class ClassLevelDirtiesContextWithDirtyMethodsTestCase extends BaseTestCase {
250+
251+
@org.testng.annotations.Test
252+
@DirtiesContext
253+
public void dirtyContext() {
254+
assertApplicationContextWasAutowired();
255+
}
256+
}
257+
258+
public static class InheritedClassLevelDirtiesContextWithDirtyMethodsTestCase extends
259+
ClassLevelDirtiesContextWithDirtyMethodsTestCase {
260+
}
261+
262+
}

0 commit comments

Comments
 (0)