Class SpringMethodRule
- java.lang.Object
- org.springframework.test.context.junit4.rules.SpringMethodRule
- All Implemented Interfaces:
MethodRule
public class SpringMethodRule extends Object implements MethodRule
SpringMethodRuleis a custom JUnitMethodRulethat supports instance-level and method-level features of the Spring TestContext Framework in standard JUnit tests by means of theTestContextManagerand associated support classes and annotations.In contrast to the
SpringJUnit4ClassRunner, Spring's rule-based JUnit support has the advantage that it is independent of anyRunnerand can therefore be combined with existing alternative runners like JUnit'sParameterizedor third-party runners such as theMockitoJUnitRunner.In order to achieve the same functionality as the
SpringJUnit4ClassRunner, however, aSpringMethodRulemust be combined with aSpringClassRule, sinceSpringMethodRuleonly supports the instance-level and method-level features of theSpringJUnit4ClassRunner.Example Usage
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 variousTestExecutionListenerorTestContextBootstrapperimplementations.)NOTE: As of Spring Framework 4.3, this class requires JUnit 4.12 or higher.
- Since:
- 4.2
- Author:
- Sam Brannen, Philippe Marschall
- See Also:
apply(Statement, FrameworkMethod, Object),SpringClassRule,TestContextManager,SpringJUnit4ClassRunner
Constructor Summary
Constructors Constructor Description SpringMethodRule()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Statementapply(Statement base, FrameworkMethod frameworkMethod, Object testInstance)Apply instance-level and method-level features of the Spring TestContext Framework to the suppliedbasestatement.
Constructor Detail
SpringMethodRule
public SpringMethodRule()
Method Detail
apply
public Statement apply(Statement base, FrameworkMethod frameworkMethod, Object testInstance)
Apply instance-level and method-level features of the Spring TestContext Framework to the suppliedbasestatement.Specifically, this method invokes the
prepareTestInstance(),beforeTestMethod(), andafterTestMethod()methods on theTestContextManager, 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
@IfProfileValueannotation from running altogether, even skipping the execution ofprepareTestInstance()methods inTestExecutionListeners.- Specified by:
applyin interfaceMethodRule- Parameters:
base- the baseStatementthat this rule should be applied toframeworkMethod- the method which is about to be invoked on the test instancetestInstance- the current test instance- Returns:
- a statement that wraps the supplied
basewith instance-level and method-level features of the Spring TestContext Framework - See Also:
withBeforeTestMethodCallbacks(org.junit.runners.model.Statement, org.junit.runners.model.FrameworkMethod, java.lang.Object, org.springframework.test.context.TestContextManager),withAfterTestMethodCallbacks(org.junit.runners.model.Statement, org.junit.runners.model.FrameworkMethod, java.lang.Object, org.springframework.test.context.TestContextManager),withPotentialRepeat(org.junit.runners.model.Statement, org.junit.runners.model.FrameworkMethod, java.lang.Object),withPotentialTimeout(org.junit.runners.model.Statement, org.junit.runners.model.FrameworkMethod, java.lang.Object),withTestInstancePreparation(org.junit.runners.model.Statement, java.lang.Object, org.springframework.test.context.TestContextManager),withProfileValueCheck(org.junit.runners.model.Statement, org.junit.runners.model.FrameworkMethod, java.lang.Object)