Skip to content

Commit d883efc

Browse files
committed
Throw meaningful exception if JUnit 4.9 is not present
SpringJUnit4ClassRunner, SpringClassRule, and SpringMethodRule now throw an IllegalStateException with a meaningful message if JUnit 4.9 is not present in the classpath (specifically if org.junit.runners.model.MultipleFailureException cannot be loaded). Issue: SPR-13521
1 parent 452b124 commit d883efc

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.apache.commons.logging.Log;
2323
import org.apache.commons.logging.LogFactory;
24+
2425
import org.junit.Ignore;
2526
import org.junit.Test;
2627
import org.junit.internal.runners.model.ReflectiveCallable;
@@ -45,6 +46,7 @@
4546
import org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks;
4647
import org.springframework.test.context.junit4.statements.SpringFailOnTimeout;
4748
import org.springframework.test.context.junit4.statements.SpringRepeat;
49+
import org.springframework.util.ClassUtils;
4850
import org.springframework.util.ReflectionUtils;
4951

5052
/**
@@ -90,7 +92,18 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner {
9092

9193
private static final Method withRulesMethod;
9294

95+
// Used by RunAfterTestClassCallbacks and RunAfterTestMethodCallbacks
96+
private static final String MULTIPLE_FAILURE_EXCEPTION_CLASS_NAME = "org.junit.runners.model.MultipleFailureException";
97+
9398
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));
105+
}
106+
94107
withRulesMethod = ReflectionUtils.findMethod(SpringJUnit4ClassRunner.class, "withRules",
95108
FrameworkMethod.class, Object.class, Statement.class);
96109
if (withRulesMethod == null) {

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks;
3535
import org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks;
3636
import org.springframework.util.Assert;
37+
import org.springframework.util.ClassUtils;
3738

3839
/**
3940
* {@code SpringClassRule} is a custom JUnit {@link TestRule} that supports
@@ -96,6 +97,19 @@ public class SpringClassRule implements TestRule {
9697
private static final Map<Class<?>, TestContextManager> testContextManagerCache =
9798
new ConcurrentHashMap<Class<?>, TestContextManager>(64);
9899

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

100114
/**
101115
* Apply <em>class-level</em> features of the <em>Spring TestContext

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.test.context.junit4.statements.RunPrepareTestInstanceCallbacks;
3434
import org.springframework.test.context.junit4.statements.SpringFailOnTimeout;
3535
import org.springframework.test.context.junit4.statements.SpringRepeat;
36+
import org.springframework.util.ClassUtils;
3637
import org.springframework.util.ReflectionUtils;
3738

3839
/**
@@ -93,6 +94,19 @@ public class SpringMethodRule implements MethodRule {
9394

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

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

97111
/**
98112
* Apply <em>instance-level</em> and <em>method-level</em> features of

0 commit comments

Comments
 (0)