Skip to content

Commit 66562f2

Browse files
committed
Require JUnit 4.12 or higher in the TestContext framework
Issue: SPR-13275
1 parent db1171d commit 66562f2

11 files changed

+43
-68
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
* and specify your runner of choice via {@link RunWith @RunWith(...)}.</li>
7171
* </ul>
7272
*
73-
* <p><strong>NOTE:</strong> As of Spring Framework 4.1, this class requires JUnit 4.9 or higher.
73+
* <p><strong>NOTE:</strong> As of Spring Framework 4.3, this class requires JUnit 4.12 or higher.
7474
*
7575
* @author Sam Brannen
7676
* @since 2.5

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
* and specify your runner of choice via {@link org.junit.runner.RunWith @RunWith(...)}.</li>
7474
* </ul>
7575
*
76-
* <p><strong>NOTE:</strong> As of Spring Framework 4.1, this class requires JUnit 4.9 or higher.
76+
* <p><strong>NOTE:</strong> As of Spring Framework 4.3, this class requires JUnit 4.12 or higher.
7777
*
7878
* @author Sam Brannen
7979
* @author Juergen Hoeller

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.lang.reflect.Field;
2020
import java.lang.reflect.Method;
21+
import java.util.concurrent.TimeUnit;
2122

2223
import org.apache.commons.logging.Log;
2324
import org.apache.commons.logging.LogFactory;
@@ -75,7 +76,7 @@
7576
* <p>If you would like to use the Spring TestContext Framework with a runner
7677
* other than this one, use {@link SpringClassRule} and {@link SpringMethodRule}.
7778
*
78-
* <p><strong>NOTE:</strong> As of Spring Framework 4.1, this class requires JUnit 4.9 or higher.
79+
* <p><strong>NOTE:</strong> As of Spring Framework 4.3, this class requires JUnit 4.12 or higher.
7980
*
8081
* @author Sam Brannen
8182
* @author Juergen Hoeller
@@ -92,27 +93,20 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner {
9293

9394
private static final Method withRulesMethod;
9495

95-
// Used by RunAfterTestClassCallbacks and RunAfterTestMethodCallbacks
96-
private static final String MULTIPLE_FAILURE_EXCEPTION_CLASS_NAME = "org.junit.runners.model.MultipleFailureException";
97-
9896
static {
99-
boolean junit4dot9Present = ClassUtils.isPresent(MULTIPLE_FAILURE_EXCEPTION_CLASS_NAME,
100-
SpringJUnit4ClassRunner.class.getClassLoader());
101-
if (!junit4dot9Present) {
102-
throw new IllegalStateException(String.format(
103-
"Failed to find class [%s]: SpringJUnit4ClassRunner requires JUnit 4.9 or higher.",
104-
MULTIPLE_FAILURE_EXCEPTION_CLASS_NAME));
97+
if (!ClassUtils.isPresent("org.junit.internal.Throwables", SpringJUnit4ClassRunner.class.getClassLoader())) {
98+
throw new IllegalStateException("SpringJUnit4ClassRunner requires JUnit 4.12 or higher.");
10599
}
106100

107101
withRulesMethod = ReflectionUtils.findMethod(SpringJUnit4ClassRunner.class, "withRules",
108102
FrameworkMethod.class, Object.class, Statement.class);
109103
if (withRulesMethod == null) {
110-
throw new IllegalStateException(
111-
"Failed to find withRules() method: SpringJUnit4ClassRunner requires JUnit 4.9 or higher.");
104+
throw new IllegalStateException("SpringJUnit4ClassRunner requires JUnit 4.12 or higher.");
112105
}
113106
ReflectionUtils.makeAccessible(withRulesMethod);
114107
}
115108

109+
116110
private final TestContextManager testContextManager;
117111

118112

@@ -376,8 +370,7 @@ else if (springTimeout > 0) {
376370
statement = new SpringFailOnTimeout(next, springTimeout);
377371
}
378372
else if (junitTimeout > 0) {
379-
// TODO Use FailOnTimeout.builder() once JUnit 4.12 is the minimum supported version.
380-
statement = new FailOnTimeout(next, junitTimeout);
373+
statement = FailOnTimeout.builder().withTimeout(junitTimeout, TimeUnit.MILLISECONDS).build(next);
381374
}
382375
else {
383376
statement = next;
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/**
2-
* Support classes for integrating the <em>Spring TestContext Framework</em> with JUnit.
2+
* Support classes for integrating the <em>Spring TestContext Framework</em>
3+
* with JUnit 4.12 or higher.
34
*/
45
package org.springframework.test.context.junit4;

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.apache.commons.logging.Log;
2525
import org.apache.commons.logging.LogFactory;
26+
2627
import org.junit.Rule;
2728
import org.junit.rules.TestRule;
2829
import org.junit.runner.Description;
@@ -76,7 +77,7 @@
7677
* <li>{@link org.springframework.test.annotation.IfProfileValue @IfProfileValue}</li>
7778
* </ul>
7879
*
79-
* <p><strong>NOTE:</strong> This class requires JUnit 4.9 or higher.
80+
* <p><strong>NOTE:</strong> As of Spring Framework 4.3, this class requires JUnit 4.12 or higher.
8081
*
8182
* @author Sam Brannen
8283
* @author Philippe Marschall
@@ -96,16 +97,9 @@ public class SpringClassRule implements TestRule {
9697
private static final Map<Class<?>, TestContextManager> testContextManagerCache =
9798
new ConcurrentHashMap<Class<?>, TestContextManager>(64);
9899

99-
// Used by RunAfterTestClassCallbacks
100-
private static final String MULTIPLE_FAILURE_EXCEPTION_CLASS_NAME = "org.junit.runners.model.MultipleFailureException";
101-
102100
static {
103-
boolean junit4dot9Present = ClassUtils.isPresent(MULTIPLE_FAILURE_EXCEPTION_CLASS_NAME,
104-
SpringClassRule.class.getClassLoader());
105-
if (!junit4dot9Present) {
106-
throw new IllegalStateException(String.format(
107-
"Failed to find class [%s]: SpringClassRule requires JUnit 4.9 or higher.",
108-
MULTIPLE_FAILURE_EXCEPTION_CLASS_NAME));
101+
if (!ClassUtils.isPresent("org.junit.internal.Throwables", SpringClassRule.class.getClassLoader())) {
102+
throw new IllegalStateException("SpringClassRule requires JUnit 4.12 or higher.");
109103
}
110104
}
111105

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.apache.commons.logging.Log;
2222
import org.apache.commons.logging.LogFactory;
23+
2324
import org.junit.ClassRule;
2425
import org.junit.rules.MethodRule;
2526
import org.junit.runners.model.FrameworkMethod;
@@ -79,7 +80,7 @@
7980
* <li>{@link org.springframework.test.annotation.IfProfileValue @IfProfileValue}</li>
8081
* </ul>
8182
*
82-
* <p><strong>NOTE:</strong> This class requires JUnit 4.9 or higher.
83+
* <p><strong>NOTE:</strong> As of Spring Framework 4.3, this class requires JUnit 4.12 or higher.
8384
*
8485
* @author Sam Brannen
8586
* @author Philippe Marschall
@@ -93,16 +94,9 @@ public class SpringMethodRule implements MethodRule {
9394

9495
private static final Log logger = LogFactory.getLog(SpringMethodRule.class);
9596

96-
// Used by RunAfterTestMethodCallbacks
97-
private static final String MULTIPLE_FAILURE_EXCEPTION_CLASS_NAME = "org.junit.runners.model.MultipleFailureException";
98-
9997
static {
100-
boolean junit4dot9Present = ClassUtils.isPresent(MULTIPLE_FAILURE_EXCEPTION_CLASS_NAME,
101-
SpringMethodRule.class.getClassLoader());
102-
if (!junit4dot9Present) {
103-
throw new IllegalStateException(String.format(
104-
"Failed to find class [%s]: SpringMethodRule requires JUnit 4.9 or higher.",
105-
MULTIPLE_FAILURE_EXCEPTION_CLASS_NAME));
98+
if (!ClassUtils.isPresent("org.junit.internal.Throwables", SpringMethodRule.class.getClassLoader())) {
99+
throw new IllegalStateException("SpringMethodRule requires JUnit 4.12 or higher.");
106100
}
107101
}
108102

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.lang.annotation.Annotation;
2020
import java.lang.reflect.Method;
2121

22-
import org.junit.Assume;
22+
import org.junit.AssumptionViolatedException;
2323
import org.junit.runners.model.Statement;
2424

2525
import org.springframework.core.annotation.AnnotationUtils;
@@ -76,27 +76,24 @@ public ProfileValueChecker(Statement next, Class<?> testClass, Method testMethod
7676
* will simply evaluate the next {@link Statement} in the execution chain.
7777
* @see ProfileValueUtils#isTestEnabledInThisEnvironment(Class)
7878
* @see ProfileValueUtils#isTestEnabledInThisEnvironment(Method, Class)
79-
* @see org.junit.Assume
79+
* @throws AssumptionViolatedException if the test is disabled
80+
* @throws Throwable if evaluation of the next statement fails
8081
*/
8182
@Override
8283
public void evaluate() throws Throwable {
8384
if (this.testMethod == null) {
8485
if (!ProfileValueUtils.isTestEnabledInThisEnvironment(this.testClass)) {
85-
// Invoke assumeTrue() with false to avoid direct reference to JUnit's
86-
// AssumptionViolatedException which exists in two packages as of JUnit 4.12.
8786
Annotation ann = AnnotationUtils.findAnnotation(this.testClass, IfProfileValue.class);
88-
Assume.assumeTrue(String.format(
89-
"Profile configured via [%s] is not enabled in this environment for test class [%s].",
90-
ann, this.testClass.getName()), false);
87+
throw new AssumptionViolatedException(
88+
String.format("Profile configured via [%s] is not enabled in this environment for test class [%s].",
89+
ann, this.testClass.getName()));
9190
}
9291
}
9392
else {
9493
if (!ProfileValueUtils.isTestEnabledInThisEnvironment(this.testMethod, this.testClass)) {
95-
// Invoke assumeTrue() with false to avoid direct reference to JUnit's
96-
// AssumptionViolatedException which exists in two packages as of JUnit 4.12.
97-
Assume.assumeTrue(String.format(
98-
"Profile configured via @IfProfileValue is not enabled in this environment for test method [%s].",
99-
this.testMethod), false);
94+
throw new AssumptionViolatedException(String.format(
95+
"Profile configured via @IfProfileValue is not enabled in this environment for test method [%s].",
96+
this.testMethod));
10097
}
10198
}
10299

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,7 @@ public void evaluate() throws Throwable {
8080
errors.add(ex);
8181
}
8282

83-
if (errors.isEmpty()) {
84-
return;
85-
}
86-
if (errors.size() == 1) {
87-
throw errors.get(0);
88-
}
89-
throw new MultipleFailureException(errors);
83+
MultipleFailureException.assertEmpty(errors);
9084
}
9185

9286
}

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,7 @@ public void evaluate() throws Throwable {
9797
errors.add(ex);
9898
}
9999

100-
if (errors.isEmpty()) {
101-
return;
102-
}
103-
if (errors.size() == 1) {
104-
throw errors.get(0);
105-
}
106-
throw new MultipleFailureException(errors);
100+
MultipleFailureException.assertEmpty(errors);
107101
}
108102

109103
}

src/asciidoc/testing.adoc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3623,8 +3623,8 @@ be automatically rolled back by the `TransactionalTestExecutionListener` (see
36233623
[[testcontext-junit4-runner]]
36243624
===== Spring JUnit Runner
36253625

3626-
The __Spring TestContext Framework__ offers full integration with JUnit 4.9+ through a
3627-
custom runner (supported on JUnit 4.9 through 4.12). By annotating test classes with
3626+
The __Spring TestContext Framework__ offers full integration with JUnit 4 through a
3627+
custom runner (supported on JUnit 4.12 or higher). By annotating test classes with
36283628
`@RunWith(SpringJUnit4ClassRunner.class)`, developers can implement standard JUnit-based
36293629
unit and integration tests and simultaneously reap the benefits of the TestContext
36303630
framework such as support for loading application contexts, dependency injection of test
@@ -3657,7 +3657,7 @@ public class SimpleTest {
36573657
===== Spring JUnit Rules
36583658

36593659
The `org.springframework.test.context.junit4.rules` package provides the following JUnit
3660-
rules.
3660+
4 rules (supported on JUnit 4.12 or higher).
36613661

36623662
* `SpringClassRule`
36633663
* `SpringMethodRule`
@@ -3701,14 +3701,14 @@ public class IntegrationTest {
37013701
===== JUnit support classes
37023702

37033703
The `org.springframework.test.context.junit4` package provides the following support
3704-
classes for JUnit-based test cases.
3704+
classes for JUnit-based test cases (supported on JUnit 4.12 or higher).
37053705

37063706
* `AbstractJUnit4SpringContextTests`
37073707
* `AbstractTransactionalJUnit4SpringContextTests`
37083708

37093709
`AbstractJUnit4SpringContextTests` is an abstract base test class that integrates the
37103710
__Spring TestContext Framework__ with explicit `ApplicationContext` testing support in
3711-
a JUnit 4.9+ environment. When you extend `AbstractJUnit4SpringContextTests`, you can
3711+
a JUnit 4 environment. When you extend `AbstractJUnit4SpringContextTests`, you can
37123712
access a `protected` `applicationContext` instance variable that can be used to perform
37133713
explicit bean lookups or to test the state of the context as a whole.
37143714

src/asciidoc/whats-new.adoc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,3 +622,11 @@ public @interface MyTestConfig {
622622
support for date header formatting via the `getDateHeader` and `setDateHeader`
623623
methods.
624624

625+
626+
627+
[[new-in-4.3]]
628+
== New Features and Enhancements in Spring Framework 4.3
629+
630+
=== Testing Improvements
631+
632+
* The JUnit support in the _Spring TestContext Framework_ now requires JUnit 4.12 or higher.

0 commit comments

Comments
 (0)