[SPR-4643] SpringJUnit4ClassRunner now optionally calls JUnit 4.7's BlockJUnit4ClassRunner.withRules() method using reflection in order to provide backward compatibility with JUnit 4.5 and 4.6.

This commit is contained in:
Sam Brannen
2009-09-07 21:03:32 +00:00
parent 7aff0755a3
commit f9f9b431a6
3 changed files with 75 additions and 2 deletions

View File

@@ -45,6 +45,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.ReflectionUtils;
/**
* <p>
@@ -307,6 +308,7 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner {
Statement statement = methodInvoker(frameworkMethod, testInstance);
statement = possiblyExpectingExceptions(frameworkMethod, testInstance, statement);
statement = withRulesReflectively(frameworkMethod, testInstance, statement);
statement = withBefores(frameworkMethod, testInstance, statement);
statement = withAfters(frameworkMethod, testInstance, statement);
statement = withPotentialRepeat(frameworkMethod, testInstance, statement);
@@ -315,6 +317,24 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner {
return statement;
}
/**
* Invokes JUnit 4.7's private <code>withRules()</code> method using
* reflection. This is necessary for backwards compatibility with the JUnit
* 4.5 and 4.6 implementations of {@link BlockJUnit4ClassRunner}.
*/
private Statement withRulesReflectively(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) {
Method withRulesMethod = ReflectionUtils.findMethod(getClass(), "withRules", FrameworkMethod.class,
Object.class, Statement.class);
if (withRulesMethod != null) {
// Original JUnit 4.7 code:
// statement = withRules(frameworkMethod, testInstance, statement);
ReflectionUtils.makeAccessible(withRulesMethod);
statement = (Statement) ReflectionUtils.invokeMethod(withRulesMethod, this, frameworkMethod, testInstance,
statement);
}
return statement;
}
/**
* Returns <code>true</code> if {@link Ignore &#064;Ignore} is present for
* the supplied {@link FrameworkMethod test method} or if the test method is