提交 d883efcb 编写于 作者: S Sam Brannen

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
上级 452b124f
......@@ -21,6 +21,7 @@ import java.lang.reflect.Method;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.internal.runners.model.ReflectiveCallable;
......@@ -45,6 +46,7 @@ import org.springframework.test.context.junit4.statements.RunBeforeTestClassCall
import org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks;
import org.springframework.test.context.junit4.statements.SpringFailOnTimeout;
import org.springframework.test.context.junit4.statements.SpringRepeat;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
/**
......@@ -90,7 +92,18 @@ 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));
}
withRulesMethod = ReflectionUtils.findMethod(SpringJUnit4ClassRunner.class, "withRules",
FrameworkMethod.class, Object.class, Statement.class);
if (withRulesMethod == null) {
......
......@@ -34,6 +34,7 @@ import org.springframework.test.context.junit4.statements.ProfileValueChecker;
import org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks;
import org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
* {@code SpringClassRule} is a custom JUnit {@link TestRule} that supports
......@@ -96,6 +97,19 @@ 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));
}
}
/**
* Apply <em>class-level</em> features of the <em>Spring TestContext
......
......@@ -33,6 +33,7 @@ import org.springframework.test.context.junit4.statements.RunBeforeTestMethodCal
import org.springframework.test.context.junit4.statements.RunPrepareTestInstanceCallbacks;
import org.springframework.test.context.junit4.statements.SpringFailOnTimeout;
import org.springframework.test.context.junit4.statements.SpringRepeat;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
/**
......@@ -93,6 +94,19 @@ 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));
}
}
/**
* Apply <em>instance-level</em> and <em>method-level</em> features of
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册