Introduce JUnitTestingUtils to reduce code duplication

This commit is contained in:
Sam Brannen 2015-05-17 15:47:39 +02:00
parent 439b64069a
commit 68322fc42a
11 changed files with 171 additions and 127 deletions

View File

@ -59,26 +59,6 @@ public class ClassLevelDirtiesContextTestNGTests {
private static final AtomicInteger cacheMisses = new AtomicInteger(0);
private static final void runTestClassAndAssertStats(Class<?> testClass, int expectedTestCount) {
final int expectedTestFailureCount = 0;
final int expectedTestStartedCount = expectedTestCount;
final int expectedTestFinishedCount = expectedTestCount;
final TrackingTestNGTestListener listener = new TrackingTestNGTestListener();
final TestNG testNG = new TestNG();
testNG.addListener(listener);
testNG.setTestClasses(new Class<?>[] { testClass });
testNG.setVerbose(0);
testNG.run();
assertEquals("Failures for test class [" + testClass + "].", expectedTestFailureCount,
listener.testFailureCount);
assertEquals("Tests started for test class [" + testClass + "].", expectedTestStartedCount,
listener.testStartCount);
assertEquals("Successful tests for test class [" + testClass + "].", expectedTestFinishedCount,
listener.testSuccessCount);
}
@BeforeClass
public static void verifyInitialCacheState() {
resetContextCache();
@ -155,6 +135,26 @@ public class ClassLevelDirtiesContextTestNGTests {
0, cacheHits.incrementAndGet(), cacheMisses.get());
}
private void runTestClassAndAssertStats(Class<?> testClass, int expectedTestCount) {
final int expectedTestFailureCount = 0;
final int expectedTestStartedCount = expectedTestCount;
final int expectedTestFinishedCount = expectedTestCount;
final TrackingTestNGTestListener listener = new TrackingTestNGTestListener();
final TestNG testNG = new TestNG();
testNG.addListener(listener);
testNG.setTestClasses(new Class<?>[] { testClass });
testNG.setVerbose(0);
testNG.run();
assertEquals("Failures for test class [" + testClass + "].", expectedTestFailureCount,
listener.testFailureCount);
assertEquals("Tests started for test class [" + testClass + "].", expectedTestStartedCount,
listener.testStartCount);
assertEquals("Successful tests for test class [" + testClass + "].", expectedTestFinishedCount,
listener.testSuccessCount);
}
private void assertBehaviorForCleanTestCase() {
runTestClassAndAssertStats(CleanTestCase.class, 1);
assertContextCacheStatistics("after clean test class", 1, cacheHits.get(), cacheMisses.incrementAndGet());

View File

@ -21,7 +21,6 @@ import java.util.concurrent.atomic.AtomicInteger;
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;
@ -33,12 +32,12 @@ import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
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;
import static org.junit.Assert.*;
import static org.springframework.test.context.cache.ContextCacheTestUtils.*;
import static org.springframework.test.context.junit4.JUnitTestingUtils.*;
/**
* JUnit 4 based integration test which verifies correct {@linkplain ContextCache
@ -56,24 +55,6 @@ public class ClassLevelDirtiesContextTests {
private static final AtomicInteger cacheMisses = new AtomicInteger(0);
private static final void runTestClassAndAssertStats(Class<?> testClass, int expectedTestCount) {
final int expectedTestFailureCount = 0;
final int expectedTestStartedCount = expectedTestCount;
final int expectedTestFinishedCount = expectedTestCount;
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() {
resetContextCache();
@ -150,7 +131,11 @@ public class ClassLevelDirtiesContextTests {
0, cacheHits.incrementAndGet(), cacheMisses.get());
}
private void assertBehaviorForCleanTestCase() {
private void runTestClassAndAssertStats(Class<?> testClass, int expectedTestCount) throws Exception {
runTestsAndAssertCounters(testClass, expectedTestCount, 0, expectedTestCount, 0, 0);
}
private void assertBehaviorForCleanTestCase() throws Exception {
runTestClassAndAssertStats(CleanTestCase.class, 1);
assertContextCacheStatistics("after clean test class", 1, cacheHits.get(), cacheMisses.incrementAndGet());
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 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.
@ -21,19 +21,15 @@ import java.util.ArrayList;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.JUnit4;
import org.springframework.test.context.TestExecutionListeners;
import static org.junit.Assert.*;
import static org.springframework.test.context.junit4.JUnitTestingUtils.*;
/**
* Verifies proper handling of the following in conjunction with the
* {@link SpringJUnit4ClassRunner}:
* <ul>
* <li>JUnit's {@link Test#expected() &#064;Test(expected=...)}</li>
* </ul>
* Verifies proper handling of JUnit's {@link Test#expected() &#064;Test(expected=...)}
* support in conjunction with the {@link SpringJUnit4ClassRunner}.
*
* @author Sam Brannen
* @since 3.0
@ -43,15 +39,7 @@ public class ExpectedExceptionSpringRunnerTests {
@Test
public void expectedExceptions() throws Exception {
Class<?> testClass = ExpectedExceptionSpringRunnerTestCase.class;
TrackingRunListener listener = new TrackingRunListener();
RunNotifier notifier = new RunNotifier();
notifier.addListener(listener);
new SpringJUnit4ClassRunner(testClass).run(notifier);
assertEquals("failures for test class [" + testClass + "].", 0, listener.getTestFailureCount());
assertEquals("tests started for test class [" + testClass + "].", 1, listener.getTestStartedCount());
assertEquals("tests finished for test class [" + testClass + "].", 1, listener.getTestFinishedCount());
runTestsAndAssertCounters(SpringJUnit4ClassRunner.class, ExpectedExceptionSpringRunnerTestCase.class, 1, 0, 1, 0, 0);
}
@ -64,7 +52,6 @@ public class ExpectedExceptionSpringRunnerTests {
public void verifyJUnitExpectedException() {
new ArrayList<Object>().get(1);
}
}
}

View File

@ -16,17 +16,13 @@
package org.springframework.test.context.junit4;
import java.lang.reflect.Constructor;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.Runner;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.springframework.beans.BeanUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestExecutionListener;
@ -38,6 +34,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ClassUtils;
import static org.junit.Assert.*;
import static org.springframework.test.context.junit4.JUnitTestingUtils.*;
/**
* JUnit 4 based integration test for verifying that '<i>before</i>' and '<i>after</i>'
@ -65,7 +62,7 @@ public class FailingBeforeAndAfterMethodsJUnitTests {
@Parameters(name = "{0}")
public static Object[] testData() {
public static Object[] testCases() {
return new Object[] {//
AlwaysFailingBeforeTestClassTestCase.class.getSimpleName(),//
AlwaysFailingAfterTestClassTestCase.class.getSimpleName(),//
@ -81,20 +78,16 @@ public class FailingBeforeAndAfterMethodsJUnitTests {
this.clazz = ClassUtils.forName(getClass().getName() + "." + testClassName, getClass().getClassLoader());
}
protected Runner getRunner(Class<?> testClass) throws Exception {
Class<? extends Runner> runnerClass = testClass.getAnnotation(RunWith.class).value();
Constructor<?> constructor = runnerClass.getConstructor(Class.class);
return (Runner) BeanUtils.instantiateClass(constructor, testClass);
protected Class<? extends Runner> getRunnerClass() {
return SpringJUnit4ClassRunner.class;
}
@Test
public void runTestAndAssertCounters() throws Exception {
TrackingRunListener listener = new TrackingRunListener();
RunNotifier notifier = new RunNotifier();
notifier.addListener(listener);
int expectedStartedCount = this.clazz.getSimpleName().startsWith("AlwaysFailingBeforeTestClass") ? 0 : 1;
int expectedFinishedCount = this.clazz.getSimpleName().startsWith("AlwaysFailingBeforeTestClass") ? 0 : 1;
getRunner(this.clazz).run(notifier);
assertEquals("Failures for test class [" + this.clazz + "].", 1, listener.getTestFailureCount());
runTestsAndAssertCounters(getRunnerClass(), this.clazz, expectedStartedCount, 1, expectedFinishedCount, 0, 0);
}

View File

@ -0,0 +1,103 @@
/*
* Copyright 2002-2015 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.junit4;
import java.lang.reflect.Constructor;
import org.junit.runner.JUnitCore;
import org.junit.runner.RunWith;
import org.junit.runner.Runner;
import org.junit.runner.notification.RunNotifier;
import org.springframework.beans.BeanUtils;
import static org.junit.Assert.*;
/**
* Collection of utilities for testing the execution of JUnit tests.
*
* @author Sam Brannen
* @since 4.2
* @see TrackingRunListener
*/
public class JUnitTestingUtils {
/**
* Run the tests in the supplied {@code testClass}, using the {@link Runner}
* it is configured with (i.e., via {@link RunWith @RunWith}) or the default
* JUnit runner, and assert the expectations of the test execution.
*
* @param testClass the test class to run with JUnit
* @param expectedStartedCount the expected number of tests that started
* @param expectedFailedCount the expected number of tests that failed
* @param expectedFinishedCount the expected number of tests that finished
* @param expectedIgnoredCount the expected number of tests that were ignored
* @param expectedAssumptionFailedCount the expected number of tests that
* resulted in a failed assumption
*/
public static void runTestsAndAssertCounters(Class<?> testClass, int expectedStartedCount, int expectedFailedCount,
int expectedFinishedCount, int expectedIgnoredCount, int expectedAssumptionFailedCount) throws Exception {
runTestsAndAssertCounters(null, testClass, expectedStartedCount, expectedFailedCount, expectedFinishedCount,
expectedIgnoredCount, expectedAssumptionFailedCount);
}
/**
* Run the tests in the supplied {@code testClass}, using the specified
* {@link Runner}, and assert the expectations of the test execution.
*
* <p>If the specified {@code runnerClass} is {@code null}, the tests
* will be run with the runner that the test class is configured with
* (i.e., via {@link RunWith @RunWith}) or the default JUnit runner.
*
* @param runnerClass the explicit runner class to use or {@code null}
* if the implicit runner should be used
* @param testClass the test class to run with JUnit
* @param expectedStartedCount the expected number of tests that started
* @param expectedFailedCount the expected number of tests that failed
* @param expectedFinishedCount the expected number of tests that finished
* @param expectedIgnoredCount the expected number of tests that were ignored
* @param expectedAssumptionFailedCount the expected number of tests that
* resulted in a failed assumption
*/
public static void runTestsAndAssertCounters(Class<? extends Runner> runnerClass, Class<?> testClass,
int expectedStartedCount, int expectedFailedCount, int expectedFinishedCount, int expectedIgnoredCount,
int expectedAssumptionFailedCount) throws Exception {
TrackingRunListener listener = new TrackingRunListener();
if (runnerClass != null) {
Constructor<?> constructor = runnerClass.getConstructor(Class.class);
Runner runner = (Runner) BeanUtils.instantiateClass(constructor, testClass);
RunNotifier notifier = new RunNotifier();
notifier.addListener(listener);
runner.run(notifier);
}
else {
JUnitCore junit = new JUnitCore();
junit.addListener(listener);
junit.run(testClass);
}
assertEquals("tests started for [" + testClass + "]:", expectedStartedCount, listener.getTestStartedCount());
assertEquals("tests failed for [" + testClass + "]:", expectedFailedCount, listener.getTestFailureCount());
assertEquals("tests finished for [" + testClass + "]:", expectedFinishedCount, listener.getTestFinishedCount());
assertEquals("tests ignored for [" + testClass + "]:", expectedIgnoredCount, listener.getTestIgnoredCount());
assertEquals("failed assumptions for [" + testClass + "]:", expectedAssumptionFailedCount, listener.getTestAssumptionFailureCount());
}
}

View File

@ -25,7 +25,6 @@ import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.Runner;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@ -35,6 +34,7 @@ import org.springframework.test.context.TestExecutionListeners;
import org.springframework.util.ClassUtils;
import static org.junit.Assert.*;
import static org.springframework.test.context.junit4.JUnitTestingUtils.*;
/**
* Verifies proper handling of the following in conjunction with the
@ -55,15 +55,14 @@ public class RepeatedSpringRunnerTests {
private final Class<?> testClass;
private final int expectedFailureCount;
private final int expectedTestStartedCount;
private final int expectedTestFinishedCount;
private final int expectedStartedCount;
private final int expectedFinishedCount;
private final int expectedInvocationCount;
@Parameters(name = "{0}")
public static Object[][] repetitionData() {
return new Object[][] {//
//
{ NonAnnotatedRepeatedTestCase.class.getSimpleName(), 0, 1, 1, 1 },//
{ DefaultRepeatValueRepeatedTestCase.class.getSimpleName(), 0, 1, 1, 1 },//
{ NegativeRepeatValueRepeatedTestCase.class.getSimpleName(), 0, 1, 1, 1 },//
@ -77,27 +76,23 @@ public class RepeatedSpringRunnerTests {
int expectedTestStartedCount, int expectedTestFinishedCount, int expectedInvocationCount) throws Exception {
this.testClass = ClassUtils.forName(getClass().getName() + "." + testClassName, getClass().getClassLoader());
this.expectedFailureCount = expectedFailureCount;
this.expectedTestStartedCount = expectedTestStartedCount;
this.expectedTestFinishedCount = expectedTestFinishedCount;
this.expectedStartedCount = expectedTestStartedCount;
this.expectedFinishedCount = expectedTestFinishedCount;
this.expectedInvocationCount = expectedInvocationCount;
}
protected Runner getRunner(Class<?> testClass) throws Exception {
return new SpringJUnit4ClassRunner(testClass);
protected Class<? extends Runner> getRunnerClass() {
return SpringJUnit4ClassRunner.class;
}
@Test
public void assertRepetitions() throws Exception {
TrackingRunListener listener = new TrackingRunListener();
RunNotifier notifier = new RunNotifier();
notifier.addListener(listener);
invocationCount.set(0);
getRunner(this.testClass).run(notifier);
assertEquals("failures for [" + testClass + "].", expectedFailureCount, listener.getTestFailureCount());
assertEquals("tests started for [" + testClass + "].", expectedTestStartedCount, listener.getTestStartedCount());
assertEquals("tests finished for [" + testClass + "].", expectedTestFinishedCount, listener.getTestFinishedCount());
assertEquals("invocations for [" + testClass + "].", expectedInvocationCount, invocationCount.get());
runTestsAndAssertCounters(getRunnerClass(), this.testClass, expectedStartedCount, expectedFailureCount,
expectedFinishedCount, 0, 0);
assertEquals("invocations for [" + testClass + "]:", expectedInvocationCount, invocationCount.get());
}

View File

@ -23,13 +23,12 @@ import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.Runner;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.JUnit4;
import org.springframework.test.annotation.Timed;
import org.springframework.test.context.TestExecutionListeners;
import static org.junit.Assert.*;
import static org.springframework.test.context.junit4.JUnitTestingUtils.*;
/**
* Verifies proper handling of the following in conjunction with the
@ -49,23 +48,13 @@ public class TimedSpringRunnerTests {
return TimedSpringRunnerTestCase.class;
}
protected Runner getRunner(Class<?> testClass) throws Exception {
return new SpringJUnit4ClassRunner(testClass);
protected Class<? extends Runner> getRunnerClass() {
return SpringJUnit4ClassRunner.class;
}
@Test
public void timedTests() throws Exception {
Class<?> testClass = getTestCase();
TrackingRunListener listener = new TrackingRunListener();
RunNotifier notifier = new RunNotifier();
notifier.addListener(listener);
getRunner(testClass).run(notifier);
assertEquals("tests started for [" + testClass + "].", 7, listener.getTestStartedCount());
assertEquals("tests ignored for [" + testClass + "].", 0, listener.getTestIgnoredCount());
assertEquals("assumption failures for [" + testClass + "].", 0, listener.getTestAssumptionFailureCount());
assertEquals("test failures for [" + testClass + "].", 5, listener.getTestFailureCount());
assertEquals("tests finished for [" + testClass + "].", 7, listener.getTestFinishedCount());
runTestsAndAssertCounters(getRunnerClass(), getTestCase(), 7, 5, 7, 0, 0);
}

View File

@ -21,6 +21,7 @@ import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.Runner;
import org.junit.runners.JUnit4;
import org.junit.runners.Parameterized.Parameters;
@ -60,6 +61,11 @@ public class FailingBeforeAndAfterMethodsSpringRuleTests extends FailingBeforeAn
super(testClassName);
}
@Override
protected Class<? extends Runner> getRunnerClass() {
return JUnit4.class;
}
// All tests are in superclass.
@RunWith(JUnit4.class)

View File

@ -46,7 +46,6 @@ public class RepeatedSpringRuleTests extends RepeatedSpringRunnerTests {
@Parameters(name = "{0}")
public static Object[][] repetitionData() {
return new Object[][] {//
//
{ NonAnnotatedRepeatedTestCase.class.getSimpleName(), 0, 1, 1, 1 },//
{ DefaultRepeatValueRepeatedTestCase.class.getSimpleName(), 0, 1, 1, 1 },//
{ NegativeRepeatValueRepeatedTestCase.class.getSimpleName(), 0, 1, 1, 1 },//
@ -64,11 +63,10 @@ public class RepeatedSpringRuleTests extends RepeatedSpringRunnerTests {
}
@Override
protected Runner getRunner(Class<?> testClass) throws Exception {
return new JUnit4(testClass);
protected Class<? extends Runner> getRunnerClass() {
return JUnit4.class;
}
// All tests are in superclass.
@TestExecutionListeners({})

View File

@ -45,8 +45,8 @@ public class TimedSpringRuleTests extends TimedSpringRunnerTests {
}
@Override
protected Runner getRunner(Class<?> testClass) throws Exception {
return new JUnit4(testClass);
protected Class<? extends Runner> getRunnerClass() {
return JUnit4.class;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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,17 +17,13 @@
package org.springframework.test.context.web;
import org.junit.Test;
import org.junit.runner.JUnitCore;
import org.springframework.test.context.junit4.TrackingRunListener;
import static org.junit.Assert.*;
import static org.springframework.test.context.junit4.JUnitTestingUtils.*;
/**
* Introduced to investigate claims in SPR-11145.
*
* <p>
* Yes, this test class does in fact use JUnit to run JUnit. ;)
* <p>Yes, this test class does in fact use JUnit to run JUnit. ;)
*
* @author Sam Brannen
* @since 4.0.2
@ -35,16 +31,8 @@ import static org.junit.Assert.*;
public class ServletContextAwareBeanWacTests {
@Test
public void ensureServletContextAwareBeanIsProcessedProperlyWhenExecutingJUnitManually() {
TrackingRunListener listener = new TrackingRunListener();
JUnitCore junit = new JUnitCore();
junit.addListener(listener);
junit.run(BasicAnnotationConfigWacTests.class);
assertEquals(3, listener.getTestStartedCount());
assertEquals(3, listener.getTestFinishedCount());
assertEquals(0, listener.getTestFailureCount());
public void ensureServletContextAwareBeanIsProcessedProperlyWhenExecutingJUnitManually() throws Exception {
runTestsAndAssertCounters(BasicAnnotationConfigWacTests.class, 3, 0, 3, 0, 0);
}
}