Require JUnit 4.12 or higher in the TestContext framework

Issue: SPR-13275
This commit is contained in:
Sam Brannen 2015-12-19 18:27:34 +01:00
parent db1171d5c4
commit 66562f2589
11 changed files with 43 additions and 68 deletions

View File

@ -70,7 +70,7 @@ import org.springframework.test.context.web.ServletTestExecutionListener;
* and specify your runner of choice via {@link RunWith @RunWith(...)}.</li>
* </ul>
*
* <p><strong>NOTE:</strong> As of Spring Framework 4.1, this class requires JUnit 4.9 or higher.
* <p><strong>NOTE:</strong> As of Spring Framework 4.3, this class requires JUnit 4.12 or higher.
*
* @author Sam Brannen
* @since 2.5

View File

@ -73,7 +73,7 @@ import org.springframework.transaction.annotation.Transactional;
* and specify your runner of choice via {@link org.junit.runner.RunWith @RunWith(...)}.</li>
* </ul>
*
* <p><strong>NOTE:</strong> As of Spring Framework 4.1, this class requires JUnit 4.9 or higher.
* <p><strong>NOTE:</strong> As of Spring Framework 4.3, this class requires JUnit 4.12 or higher.
*
* @author Sam Brannen
* @author Juergen Hoeller

View File

@ -18,6 +18,7 @@ package org.springframework.test.context.junit4;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -75,7 +76,7 @@ import org.springframework.util.ReflectionUtils;
* <p>If you would like to use the Spring TestContext Framework with a runner
* other than this one, use {@link SpringClassRule} and {@link SpringMethodRule}.
*
* <p><strong>NOTE:</strong> As of Spring Framework 4.1, this class requires JUnit 4.9 or higher.
* <p><strong>NOTE:</strong> As of Spring Framework 4.3, this class requires JUnit 4.12 or higher.
*
* @author Sam Brannen
* @author Juergen Hoeller
@ -92,27 +93,20 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner {
private static final Method withRulesMethod;
// Used by RunAfterTestClassCallbacks and RunAfterTestMethodCallbacks
private static final String MULTIPLE_FAILURE_EXCEPTION_CLASS_NAME = "org.junit.runners.model.MultipleFailureException";
static {
boolean junit4dot9Present = ClassUtils.isPresent(MULTIPLE_FAILURE_EXCEPTION_CLASS_NAME,
SpringJUnit4ClassRunner.class.getClassLoader());
if (!junit4dot9Present) {
throw new IllegalStateException(String.format(
"Failed to find class [%s]: SpringJUnit4ClassRunner requires JUnit 4.9 or higher.",
MULTIPLE_FAILURE_EXCEPTION_CLASS_NAME));
if (!ClassUtils.isPresent("org.junit.internal.Throwables", SpringJUnit4ClassRunner.class.getClassLoader())) {
throw new IllegalStateException("SpringJUnit4ClassRunner requires JUnit 4.12 or higher.");
}
withRulesMethod = ReflectionUtils.findMethod(SpringJUnit4ClassRunner.class, "withRules",
FrameworkMethod.class, Object.class, Statement.class);
if (withRulesMethod == null) {
throw new IllegalStateException(
"Failed to find withRules() method: SpringJUnit4ClassRunner requires JUnit 4.9 or higher.");
throw new IllegalStateException("SpringJUnit4ClassRunner requires JUnit 4.12 or higher.");
}
ReflectionUtils.makeAccessible(withRulesMethod);
}
private final TestContextManager testContextManager;
@ -376,8 +370,7 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner {
statement = new SpringFailOnTimeout(next, springTimeout);
}
else if (junitTimeout > 0) {
// TODO Use FailOnTimeout.builder() once JUnit 4.12 is the minimum supported version.
statement = new FailOnTimeout(next, junitTimeout);
statement = FailOnTimeout.builder().withTimeout(junitTimeout, TimeUnit.MILLISECONDS).build(next);
}
else {
statement = next;

View File

@ -1,4 +1,5 @@
/**
* Support classes for integrating the <em>Spring TestContext Framework</em> with JUnit.
* Support classes for integrating the <em>Spring TestContext Framework</em>
* with JUnit 4.12 or higher.
*/
package org.springframework.test.context.junit4;

View File

@ -23,6 +23,7 @@ import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Rule;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
@ -76,7 +77,7 @@ import org.springframework.util.ClassUtils;
* <li>{@link org.springframework.test.annotation.IfProfileValue @IfProfileValue}</li>
* </ul>
*
* <p><strong>NOTE:</strong> This class requires JUnit 4.9 or higher.
* <p><strong>NOTE:</strong> As of Spring Framework 4.3, this class requires JUnit 4.12 or higher.
*
* @author Sam Brannen
* @author Philippe Marschall
@ -96,16 +97,9 @@ public class SpringClassRule implements TestRule {
private static final Map<Class<?>, TestContextManager> testContextManagerCache =
new ConcurrentHashMap<Class<?>, TestContextManager>(64);
// Used by RunAfterTestClassCallbacks
private static final String MULTIPLE_FAILURE_EXCEPTION_CLASS_NAME = "org.junit.runners.model.MultipleFailureException";
static {
boolean junit4dot9Present = ClassUtils.isPresent(MULTIPLE_FAILURE_EXCEPTION_CLASS_NAME,
SpringClassRule.class.getClassLoader());
if (!junit4dot9Present) {
throw new IllegalStateException(String.format(
"Failed to find class [%s]: SpringClassRule requires JUnit 4.9 or higher.",
MULTIPLE_FAILURE_EXCEPTION_CLASS_NAME));
if (!ClassUtils.isPresent("org.junit.internal.Throwables", SpringClassRule.class.getClassLoader())) {
throw new IllegalStateException("SpringClassRule requires JUnit 4.12 or higher.");
}
}

View File

@ -20,6 +20,7 @@ import java.lang.reflect.Field;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.ClassRule;
import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
@ -79,7 +80,7 @@ import org.springframework.util.ReflectionUtils;
* <li>{@link org.springframework.test.annotation.IfProfileValue @IfProfileValue}</li>
* </ul>
*
* <p><strong>NOTE:</strong> This class requires JUnit 4.9 or higher.
* <p><strong>NOTE:</strong> As of Spring Framework 4.3, this class requires JUnit 4.12 or higher.
*
* @author Sam Brannen
* @author Philippe Marschall
@ -93,16 +94,9 @@ public class SpringMethodRule implements MethodRule {
private static final Log logger = LogFactory.getLog(SpringMethodRule.class);
// Used by RunAfterTestMethodCallbacks
private static final String MULTIPLE_FAILURE_EXCEPTION_CLASS_NAME = "org.junit.runners.model.MultipleFailureException";
static {
boolean junit4dot9Present = ClassUtils.isPresent(MULTIPLE_FAILURE_EXCEPTION_CLASS_NAME,
SpringMethodRule.class.getClassLoader());
if (!junit4dot9Present) {
throw new IllegalStateException(String.format(
"Failed to find class [%s]: SpringMethodRule requires JUnit 4.9 or higher.",
MULTIPLE_FAILURE_EXCEPTION_CLASS_NAME));
if (!ClassUtils.isPresent("org.junit.internal.Throwables", SpringMethodRule.class.getClassLoader())) {
throw new IllegalStateException("SpringMethodRule requires JUnit 4.12 or higher.");
}
}

View File

@ -19,7 +19,7 @@ package org.springframework.test.context.junit4.statements;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import org.junit.Assume;
import org.junit.AssumptionViolatedException;
import org.junit.runners.model.Statement;
import org.springframework.core.annotation.AnnotationUtils;
@ -76,27 +76,24 @@ public class ProfileValueChecker extends Statement {
* will simply evaluate the next {@link Statement} in the execution chain.
* @see ProfileValueUtils#isTestEnabledInThisEnvironment(Class)
* @see ProfileValueUtils#isTestEnabledInThisEnvironment(Method, Class)
* @see org.junit.Assume
* @throws AssumptionViolatedException if the test is disabled
* @throws Throwable if evaluation of the next statement fails
*/
@Override
public void evaluate() throws Throwable {
if (this.testMethod == null) {
if (!ProfileValueUtils.isTestEnabledInThisEnvironment(this.testClass)) {
// Invoke assumeTrue() with false to avoid direct reference to JUnit's
// AssumptionViolatedException which exists in two packages as of JUnit 4.12.
Annotation ann = AnnotationUtils.findAnnotation(this.testClass, IfProfileValue.class);
Assume.assumeTrue(String.format(
"Profile configured via [%s] is not enabled in this environment for test class [%s].",
ann, this.testClass.getName()), false);
throw new AssumptionViolatedException(
String.format("Profile configured via [%s] is not enabled in this environment for test class [%s].",
ann, this.testClass.getName()));
}
}
else {
if (!ProfileValueUtils.isTestEnabledInThisEnvironment(this.testMethod, this.testClass)) {
// Invoke assumeTrue() with false to avoid direct reference to JUnit's
// AssumptionViolatedException which exists in two packages as of JUnit 4.12.
Assume.assumeTrue(String.format(
"Profile configured via @IfProfileValue is not enabled in this environment for test method [%s].",
this.testMethod), false);
throw new AssumptionViolatedException(String.format(
"Profile configured via @IfProfileValue is not enabled in this environment for test method [%s].",
this.testMethod));
}
}

View File

@ -80,13 +80,7 @@ public class RunAfterTestClassCallbacks extends Statement {
errors.add(ex);
}
if (errors.isEmpty()) {
return;
}
if (errors.size() == 1) {
throw errors.get(0);
}
throw new MultipleFailureException(errors);
MultipleFailureException.assertEmpty(errors);
}
}

View File

@ -97,13 +97,7 @@ public class RunAfterTestMethodCallbacks extends Statement {
errors.add(ex);
}
if (errors.isEmpty()) {
return;
}
if (errors.size() == 1) {
throw errors.get(0);
}
throw new MultipleFailureException(errors);
MultipleFailureException.assertEmpty(errors);
}
}

View File

@ -3623,8 +3623,8 @@ be automatically rolled back by the `TransactionalTestExecutionListener` (see
[[testcontext-junit4-runner]]
===== Spring JUnit Runner
The __Spring TestContext Framework__ offers full integration with JUnit 4.9+ through a
custom runner (supported on JUnit 4.9 through 4.12). By annotating test classes with
The __Spring TestContext Framework__ offers full integration with JUnit 4 through a
custom runner (supported on JUnit 4.12 or higher). By annotating test classes with
`@RunWith(SpringJUnit4ClassRunner.class)`, developers can implement standard JUnit-based
unit and integration tests and simultaneously reap the benefits of the TestContext
framework such as support for loading application contexts, dependency injection of test
@ -3657,7 +3657,7 @@ public class SimpleTest {
===== Spring JUnit Rules
The `org.springframework.test.context.junit4.rules` package provides the following JUnit
rules.
4 rules (supported on JUnit 4.12 or higher).
* `SpringClassRule`
* `SpringMethodRule`
@ -3701,14 +3701,14 @@ public class IntegrationTest {
===== JUnit support classes
The `org.springframework.test.context.junit4` package provides the following support
classes for JUnit-based test cases.
classes for JUnit-based test cases (supported on JUnit 4.12 or higher).
* `AbstractJUnit4SpringContextTests`
* `AbstractTransactionalJUnit4SpringContextTests`
`AbstractJUnit4SpringContextTests` is an abstract base test class that integrates the
__Spring TestContext Framework__ with explicit `ApplicationContext` testing support in
a JUnit 4.9+ environment. When you extend `AbstractJUnit4SpringContextTests`, you can
a JUnit 4 environment. When you extend `AbstractJUnit4SpringContextTests`, you can
access a `protected` `applicationContext` instance variable that can be used to perform
explicit bean lookups or to test the state of the context as a whole.

View File

@ -622,3 +622,11 @@ public @interface MyTestConfig {
support for date header formatting via the `getDateHeader` and `setDateHeader`
methods.
[[new-in-4.3]]
== New Features and Enhancements in Spring Framework 4.3
=== Testing Improvements
* The JUnit support in the _Spring TestContext Framework_ now requires JUnit 4.12 or higher.