public class SpringMethodRule
extends java.lang.Object
SpringMethodRule is a custom JUnit 4 MethodRule that
supports instance-level and method-level features of the
Spring TestContext Framework in standard JUnit tests by means
of the TestContextManager and associated support classes and
annotations.
In contrast to the SpringJUnit4ClassRunner, Spring's rule-based JUnit support has the advantage
that it is independent of any Runner and
can therefore be combined with existing alternative runners like JUnit's
Parameterized or third-party runners such as the MockitoJUnitRunner.
In order to achieve the same functionality as the SpringJUnit4ClassRunner,
however, a SpringMethodRule must be combined with a SpringClassRule,
since SpringMethodRule only supports the instance-level and method-level
features of the SpringJUnit4ClassRunner.
public class ExampleSpringIntegrationTest {
@ClassRule
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule();
// ...
}
The following list constitutes all annotations currently supported directly
or indirectly by SpringMethodRule. (Note that additional annotations
may be supported by various
TestExecutionListener or
TestContextBootstrapper
implementations.)
NOTE: As of Spring Framework 4.3, this class requires JUnit 4.12 or higher.
WARNING: Due to the shortcomings of JUnit rules, the
SpringMethodRule does not support the
beforeTestExecution() and afterTestExecution() callbacks of the
TestExecutionListener
API.
apply(Statement, FrameworkMethod, Object),
SpringClassRule,
TestContextManager,
SpringJUnit4ClassRunner| Modifier and Type | Field and Description |
|---|---|
private static Log |
logger |
| Constructor and Description |
|---|
SpringMethodRule() |
| Modifier and Type | Method and Description |
|---|---|
Statement |
apply(Statement base,
FrameworkMethod frameworkMethod,
java.lang.Object testInstance)
Apply instance-level and method-level features of
the Spring TestContext Framework to the supplied
base
statement. |
private static java.util.Optional<java.lang.reflect.Field> |
findSpringClassRuleField(java.lang.Class<?> testClass) |
private static SpringClassRule |
validateSpringClassRuleConfiguration(java.lang.Class<?> testClass)
Throw an
IllegalStateException if the supplied testClass
does not declare a public static final SpringClassRule field
that is annotated with @ClassRule. |
private Statement |
withAfterTestMethodCallbacks(Statement next,
java.lang.reflect.Method testMethod,
java.lang.Object testInstance,
TestContextManager testContextManager)
Wrap the supplied
Statement with a RunAfterTestMethodCallbacks statement. |
private Statement |
withBeforeTestMethodCallbacks(Statement next,
java.lang.reflect.Method testMethod,
java.lang.Object testInstance,
TestContextManager testContextManager)
Wrap the supplied
Statement with a RunBeforeTestMethodCallbacks statement. |
private Statement |
withPotentialRepeat(Statement next,
java.lang.reflect.Method testMethod,
java.lang.Object testInstance)
Wrap the supplied
Statement with a SpringRepeat statement. |
private Statement |
withPotentialTimeout(Statement next,
java.lang.reflect.Method testMethod,
java.lang.Object testInstance)
Wrap the supplied
Statement with a SpringFailOnTimeout statement. |
private Statement |
withProfileValueCheck(Statement next,
java.lang.reflect.Method testMethod,
java.lang.Object testInstance)
Wrap the supplied
Statement with a ProfileValueChecker statement. |
private Statement |
withTestInstancePreparation(Statement next,
java.lang.Object testInstance,
TestContextManager testContextManager)
Wrap the supplied
Statement with a RunPrepareTestInstanceCallbacks statement. |
public Statement apply(Statement base,
FrameworkMethod frameworkMethod,
java.lang.Object testInstance)
base
statement.
Specifically, this method invokes the
prepareTestInstance(),
beforeTestMethod(), and
afterTestMethod() methods
on the TestContextManager, potentially with Spring timeouts
and repetitions.
In addition, this method checks whether the test is enabled in
the current execution environment. This prevents methods with a
non-matching @IfProfileValue annotation from running altogether,
even skipping the execution of prepareTestInstance() methods
in TestExecutionListeners.
base - the base Statement that this rule should be applied toframeworkMethod - the method which is about to be invoked on the test instancetestInstance - the current test instancebase with instance-level
and method-level features of the Spring TestContext FrameworkwithBeforeTestMethodCallbacks(Statement, java.lang.reflect.Method, java.lang.Object, org.springframework.test.context.TestContextManager),
withAfterTestMethodCallbacks(Statement, java.lang.reflect.Method, java.lang.Object, org.springframework.test.context.TestContextManager),
withPotentialRepeat(Statement, java.lang.reflect.Method, java.lang.Object),
withPotentialTimeout(Statement, java.lang.reflect.Method, java.lang.Object),
withTestInstancePreparation(Statement, java.lang.Object, org.springframework.test.context.TestContextManager),
withProfileValueCheck(Statement, java.lang.reflect.Method, java.lang.Object)private Statement withBeforeTestMethodCallbacks(Statement next,
java.lang.reflect.Method testMethod,
java.lang.Object testInstance,
TestContextManager testContextManager)
Statement with a RunBeforeTestMethodCallbacks statement.RunBeforeTestMethodCallbacksprivate Statement withAfterTestMethodCallbacks(Statement next,
java.lang.reflect.Method testMethod,
java.lang.Object testInstance,
TestContextManager testContextManager)
Statement with a RunAfterTestMethodCallbacks statement.RunAfterTestMethodCallbacksprivate Statement withTestInstancePreparation(Statement next,
java.lang.Object testInstance,
TestContextManager testContextManager)
Statement with a RunPrepareTestInstanceCallbacks statement.RunPrepareTestInstanceCallbacksprivate Statement withPotentialRepeat(Statement next,
java.lang.reflect.Method testMethod,
java.lang.Object testInstance)
SpringRepeatprivate Statement withPotentialTimeout(Statement next,
java.lang.reflect.Method testMethod,
java.lang.Object testInstance)
Statement with a SpringFailOnTimeout statement.
Supports Spring's @Timed
annotation.
SpringFailOnTimeoutprivate Statement withProfileValueCheck(Statement next,
java.lang.reflect.Method testMethod,
java.lang.Object testInstance)
Statement with a ProfileValueChecker statement.ProfileValueCheckerprivate static SpringClassRule validateSpringClassRuleConfiguration(java.lang.Class<?> testClass)
IllegalStateException if the supplied testClass
does not declare a public static final SpringClassRule field
that is annotated with @ClassRule.private static java.util.Optional<java.lang.reflect.Field> findSpringClassRuleField(java.lang.Class<?> testClass)