spring-framework / org.springframework.test.context.junit4.rules / SpringMethodRule

SpringMethodRule

open class SpringMethodRule : MethodRule

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 org.springframework.test.context.junit4.SpringJUnit4ClassRunner, Spring's rule-based JUnit support has the advantage that it is independent of any org.junit.runner.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.

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 various org.springframework.test.context.TestExecutionListener or org.springframework.test.context.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 org.springframework.test.context.TestExecutionListener API.

Author
Sam Brannen

Author
Philippe Marschall

Since
4.2

See Also
#apply(Statement, FrameworkMethod, Object)SpringClassRuleorg.springframework.test.context.TestContextManagerorg.springframework.test.context.junit4.SpringJUnit4ClassRunner

Constructors

<init>

SpringMethodRule()

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 org.springframework.test.context.junit4.SpringJUnit4ClassRunner, Spring's rule-based JUnit support has the advantage that it is independent of any org.junit.runner.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.

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 various org.springframework.test.context.TestExecutionListener or org.springframework.test.context.TestContextBootstrapper implementations.)

  • org.springframework.test.annotation.Timed
  • org.springframework.test.annotation.Repeat
  • org.springframework.test.annotation.ProfileValueSourceConfiguration
  • org.springframework.test.annotation.IfProfileValue

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 org.springframework.test.context.TestExecutionListener API.

Functions

apply

open fun apply(base: Statement, frameworkMethod: FrameworkMethod, testInstance: Any): Statement

Apply instance-level and method-level features of the Spring TestContext Framework to the supplied 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.