类 SpringMethodRule
- java.lang.Object
- org.springframework.test.context.junit4.rules.SpringMethodRule
- 所有已实现的接口:
MethodRule
public class SpringMethodRule extends Object implements MethodRule
SpringMethodRule
is a custom JUnit 4MethodRule
that supports instance-level and method-level features of the Spring TestContext Framework in standard JUnit tests by means of theTestContextManager
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 anyRunner
and can therefore be combined with existing alternative runners like JUnit'sParameterized
or third-party runners such as theMockitoJUnitRunner
.In order to achieve the same functionality as the
SpringJUnit4ClassRunner
, however, aSpringMethodRule
must be combined with aSpringClassRule
, sinceSpringMethodRule
only supports the instance-level and method-level features of theSpringJUnit4ClassRunner
.Example Usage
public class ExampleSpringIntegrationTest { @ClassRule public static final SpringClassRule springClassRule = 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 variousTestExecutionListener
orTestContextBootstrapper
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 thebeforeTestExecution()
andafterTestExecution()
callbacks of theTestExecutionListener
API.- 从以下版本开始:
- 4.2
- 作者:
- Sam Brannen, Philippe Marschall
- 另请参阅:
apply(Statement, FrameworkMethod, Object)
,SpringClassRule
,TestContextManager
,SpringJUnit4ClassRunner
构造器概要
构造器 构造器 说明 SpringMethodRule()
构造器详细资料
SpringMethodRule
public SpringMethodRule()
方法详细资料
apply
public Statement apply(Statement base, FrameworkMethod frameworkMethod, Object testInstance)
Apply instance-level and method-level features of the Spring TestContext Framework to the suppliedbase
statement.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
@IfProfileValue
annotation from running altogether, even skipping the execution ofprepareTestInstance()
methods inTestExecutionListeners
.- 指定者:
apply
在接口中MethodRule
- 参数:
base
- the baseStatement
that this rule should be applied toframeworkMethod
- the method which is about to be invoked on the test instancetestInstance
- the current test instance- 返回:
- a statement that wraps the supplied
base
with instance-level and method-level features of the Spring TestContext Framework - 另请参阅:
withBeforeTestMethodCallbacks(org.junit.runners.model.Statement, java.lang.reflect.Method, java.lang.Object, org.springframework.test.context.TestContextManager)
,withAfterTestMethodCallbacks(org.junit.runners.model.Statement, java.lang.reflect.Method, java.lang.Object, org.springframework.test.context.TestContextManager)
,withPotentialRepeat(org.junit.runners.model.Statement, java.lang.reflect.Method, java.lang.Object)
,withPotentialTimeout(org.junit.runners.model.Statement, java.lang.reflect.Method, java.lang.Object)
,withTestInstancePreparation(org.junit.runners.model.Statement, java.lang.Object, org.springframework.test.context.TestContextManager)
,withProfileValueCheck(org.junit.runners.model.Statement, java.lang.reflect.Method, java.lang.Object)