[SPR-4702] Added support for @DirtiesContext at the test class level.

This commit is contained in:
Sam Brannen
2009-07-06 12:58:56 +00:00
parent 0222a4a56c
commit 8dec6af038
17 changed files with 714 additions and 135 deletions

View File

@@ -0,0 +1,174 @@
/*
* Copyright 2002-2009 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;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.JUnitCore;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.TrackingRunListener;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
/**
* JUnit 4 based integration test which verifies correct {@link ContextCache
* application context caching} in conjunction with the
* {@link SpringJUnit4ClassRunner} and the {@link DirtiesContext
* @DirtiesContext} annotation at the class level.
*
* @author Sam Brannen
* @since 3.0
*/
@RunWith(JUnit4.class)
public class ClassLevelDirtiesContextTests {
/**
* Asserts the statistics of the supplied context cache.
*
* @param usageScenario the scenario in which the statistics are used
* @param expectedSize the expected number of contexts in the cache
* @param expectedHitCount the expected hit count
* @param expectedMissCount the expected miss count
*/
private static final void assertContextCacheStatistics(String usageScenario, int expectedSize,
int expectedHitCount, int expectedMissCount) {
ContextCache contextCache = TestContextManager.contextCache;
assertEquals("Verifying number of contexts in cache (" + usageScenario + ").", expectedSize,
contextCache.size());
assertEquals("Verifying number of cache hits (" + usageScenario + ").", expectedHitCount,
contextCache.getHitCount());
assertEquals("Verifying number of cache misses (" + usageScenario + ").", expectedMissCount,
contextCache.getMissCount());
}
private static final void runTestClassAndAssertRunListenerStats(Class<?> testClass) {
final int expectedTestFailureCount = 0;
final int expectedTestStartedCount = 1;
final int expectedTestFinishedCount = 1;
TrackingRunListener listener = new TrackingRunListener();
JUnitCore jUnitCore = new JUnitCore();
jUnitCore.addListener(listener);
jUnitCore.run(testClass);
assertEquals("Verifying number of failures for test class [" + testClass + "].", expectedTestFailureCount,
listener.getTestFailureCount());
assertEquals("Verifying number of tests started for test class [" + testClass + "].", expectedTestStartedCount,
listener.getTestStartedCount());
assertEquals("Verifying number of tests finished for test class [" + testClass + "].",
expectedTestFinishedCount, listener.getTestFinishedCount());
}
@BeforeClass
public static void verifyInitialCacheState() {
ContextCache contextCache = TestContextManager.contextCache;
contextCache.clear();
contextCache.clearStatistics();
assertContextCacheStatistics("BeforeClass", 0, 0, 0);
}
@AfterClass
public static void verifyFinalCacheState() {
assertContextCacheStatistics("AfterClass", 0, 3, 5);
}
@Test
public void verifyDirtiesContextBehavior() throws Exception {
int hits = 0;
int misses = 0;
runTestClassAndAssertRunListenerStats(CleanTestCase.class);
assertContextCacheStatistics("after clean test class", 1, hits, ++misses);
runTestClassAndAssertRunListenerStats(ClassLevelDirtiesContextWithCleanMethodsTestCase.class);
assertContextCacheStatistics("after class-level @DirtiesContext with clean test method", 0, ++hits, misses);
runTestClassAndAssertRunListenerStats(CleanTestCase.class);
assertContextCacheStatistics("after clean test class", 1, hits, ++misses);
runTestClassAndAssertRunListenerStats(ClassLevelDirtiesContextWithDirtyMethodsTestCase.class);
assertContextCacheStatistics("after class-level @DirtiesContext with dirty test method", 0, ++hits, misses);
runTestClassAndAssertRunListenerStats(ClassLevelDirtiesContextWithDirtyMethodsTestCase.class);
assertContextCacheStatistics("after class-level @DirtiesContext with dirty test method", 0, hits, ++misses);
runTestClassAndAssertRunListenerStats(ClassLevelDirtiesContextWithDirtyMethodsTestCase.class);
assertContextCacheStatistics("after class-level @DirtiesContext with dirty test method", 0, hits, ++misses);
runTestClassAndAssertRunListenerStats(CleanTestCase.class);
assertContextCacheStatistics("after clean test class", 1, hits, ++misses);
runTestClassAndAssertRunListenerStats(ClassLevelDirtiesContextWithCleanMethodsTestCase.class);
assertContextCacheStatistics("after class-level @DirtiesContext with clean test method", 0, ++hits, misses);
}
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class })
@ContextConfiguration("/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerAppCtxTests-context.xml")
public static abstract class BaseTestCase {
@Autowired
protected ApplicationContext applicationContext;
protected void assertApplicationContextWasAutowired() {
assertNotNull("The application context should have been autowired.", this.applicationContext);
}
}
public static final class CleanTestCase extends BaseTestCase {
@Test
public void verifyContextWasAutowired() {
assertApplicationContextWasAutowired();
}
}
@DirtiesContext
public static final class ClassLevelDirtiesContextWithCleanMethodsTestCase extends BaseTestCase {
@Test
public void verifyContextWasAutowired() {
assertApplicationContextWasAutowired();
}
}
@DirtiesContext
public static final class ClassLevelDirtiesContextWithDirtyMethodsTestCase extends BaseTestCase {
@Test
@DirtiesContext
public void dirtyContext() {
assertApplicationContextWasAutowired();
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2009 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.
@@ -25,47 +25,49 @@ import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* JUnit 4 based unit test which verifies correct
* {@link ContextCache application context caching} in conjunction with the
* {@link SpringJUnit4ClassRunner} and the {@link DirtiesContext} annotation.
*
* JUnit 4 based unit test which verifies correct {@link ContextCache
* application context caching} in conjunction with the
* {@link SpringJUnit4ClassRunner} and the {@link DirtiesContext
* &#064;DirtiesContext} annotation at the method level.
*
* @author Sam Brannen
* @author Juergen Hoeller
* @since 2.5
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerAppCtxTests-context.xml")
public class SpringRunnerContextCacheTests implements ApplicationContextAware {
@ContextConfiguration("/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerAppCtxTests-context.xml")
public class SpringRunnerContextCacheTests {
private static ApplicationContext dirtiedApplicationContext;
@Autowired
protected ApplicationContext applicationContext;
/**
* Asserts the statistics of the supplied context cache.
*
* @param usageScenario the scenario in which the statistics are used
* @param expectedSize the expected number of contexts in the cache
* @param expectedHitCount the expected hit count
* @param expectedMissCount the expected miss count
*/
public static final void assertContextCacheStatistics(String usageScenario, int expectedSize,
int expectedHitCount, int expectedMissCount) {
public static final void assertContextCacheStatistics(String usageScenario, int expectedSize, int expectedHitCount,
int expectedMissCount) {
ContextCache contextCache = TestContextManager.contextCache;
assertEquals("Verifying number of contexts in cache (" + usageScenario + ").", expectedSize,
contextCache.size());
contextCache.size());
assertEquals("Verifying number of cache hits (" + usageScenario + ").", expectedHitCount,
contextCache.getHitCount());
contextCache.getHitCount());
assertEquals("Verifying number of cache misses (" + usageScenario + ").", expectedMissCount,
contextCache.getMissCount());
contextCache.getMissCount());
}
@BeforeClass
@@ -82,37 +84,29 @@ public class SpringRunnerContextCacheTests implements ApplicationContextAware {
assertContextCacheStatistics("AfterClass", 1, 1, 2);
}
public final void setApplicationContext(final ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Test
@DirtiesContext
public void dirtyContext() {
assertContextCacheStatistics("dirtyContext()", 1, 0, 1);
assertNotNull("The application context should have been set due to ApplicationContextAware semantics.",
this.applicationContext);
assertNotNull("The application context should have been autowired.", this.applicationContext);
SpringRunnerContextCacheTests.dirtiedApplicationContext = this.applicationContext;
}
@Test
public void verifyContextWasDirtied() {
assertContextCacheStatistics("verifyContextWasDirtied()", 1, 0, 2);
assertNotNull("The application context should have been set due to ApplicationContextAware semantics.",
this.applicationContext);
assertNotNull("The application context should have been autowired.", this.applicationContext);
assertNotSame("The application context should have been 'dirtied'.",
SpringRunnerContextCacheTests.dirtiedApplicationContext, this.applicationContext);
SpringRunnerContextCacheTests.dirtiedApplicationContext, this.applicationContext);
SpringRunnerContextCacheTests.dirtiedApplicationContext = this.applicationContext;
}
@Test
public void verifyContextWasNotDirtied() {
assertContextCacheStatistics("verifyContextWasNotDirtied()", 1, 1, 2);
assertNotNull("The application context should have been set due to ApplicationContextAware semantics.",
this.applicationContext);
assertNotNull("The application context should have been autowired.", this.applicationContext);
assertSame("The application context should NOT have been 'dirtied'.",
SpringRunnerContextCacheTests.dirtiedApplicationContext, this.applicationContext);
SpringRunnerContextCacheTests.dirtiedApplicationContext, this.applicationContext);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2009 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.
@@ -17,9 +17,9 @@
package org.springframework.test.context.junit4;
import static org.junit.Assert.fail;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.annotation.IfProfileValue;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestExecutionListener;
@@ -27,6 +27,7 @@ import org.springframework.test.context.TestExecutionListeners;
/**
* @author Juergen Hoeller
* @author Sam Brannen
*/
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners(ClassLevelDisabledSpringRunnerTests.CustomTestExecutionListener.class)
@@ -41,6 +42,10 @@ public class ClassLevelDisabledSpringRunnerTests {
public static class CustomTestExecutionListener implements TestExecutionListener {
public void beforeTestClass(TestContext testContext) throws Exception {
fail("A listener method for a disabled test should never be executed!");
}
public void prepareTestInstance(TestContext testContext) throws Exception {
fail("A listener method for a disabled test should never be executed!");
}
@@ -52,6 +57,9 @@ public class ClassLevelDisabledSpringRunnerTests {
public void afterTestMethod(TestContext testContext) throws Exception {
fail("A listener method for a disabled test should never be executed!");
}
}
public void afterTestClass(TestContext testContext) throws Exception {
fail("A listener method for a disabled test should never be executed!");
}
}
}

View File

@@ -19,6 +19,7 @@ package org.springframework.test.context.junit4;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import org.springframework.test.context.ClassLevelDirtiesContextTests;
import org.springframework.test.context.SpringRunnerContextCacheTests;
/**
@@ -59,6 +60,7 @@ StandardJUnit4FeaturesTests.class,//
PropertiesBasedSpringJUnit4ClassRunnerAppCtxTests.class,//
CustomDefaultContextLoaderClassSpringRunnerTests.class,//
SpringRunnerContextCacheTests.class,//
ClassLevelDirtiesContextTests.class,//
ParameterizedDependencyInjectionTests.class,//
ClassLevelTransactionalSpringRunnerTests.class,//
MethodLevelTransactionalSpringRunnerTests.class,//

View File

@@ -51,7 +51,6 @@ import org.testng.annotations.Test;
* @author Sam Brannen
* @since 2.5
*/
@org.junit.Ignore("TestNG tests should not be run by JUnit")
@ContextConfiguration
public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTransactionalTestNGSpringContextTests
implements BeanNameAware, InitializingBean {

View File

@@ -32,7 +32,7 @@ import org.testng.annotations.Test;
* TestNG based unit test to assess the claim in <a
* href="http://opensource.atlassian.com/projects/spring/browse/SPR-3880"
* target="_blank">SPR-3880</a> that a &quot;context marked dirty using
* {@link DirtiesContext @DirtiesContext} in [a] TestNG based test is not
* {@link DirtiesContext &#064;DirtiesContext} in [a] TestNG based test is not
* reloaded in subsequent tests&quot;.
* </p>
* <p>
@@ -47,7 +47,6 @@ import org.testng.annotations.Test;
* @author Sam Brannen
* @since 2.5
*/
@org.junit.Ignore("TestNG tests should not be run by JUnit")
@ContextConfiguration
public class DirtiesContextTransactionalTestNGSpringContextTests extends AbstractTransactionalTestNGSpringContextTests {
@@ -82,4 +81,5 @@ public class DirtiesContextTransactionalTestNGSpringContextTests extends Abstrac
assertSame(this.applicationContext, this.dirtiedApplicationContext,
"The application context should NOT have been 'dirtied'.");
}
}