Class SpringClassRule
- All Implemented Interfaces:
TestRule
SpringClassRule is a custom JUnit TestRule that supports
class-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 SpringClassRule must be combined with a SpringMethodRule,
since SpringClassRule only supports the class-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 SpringClassRule. (Note that additional annotations
may be supported by various
TestExecutionListener or
TestContextBootstrapper
implementations.)
NOTE: This class requires JUnit 4.12 or higher.
- Since:
- 4.2
- Author:
- Sam Brannen, Philippe Marschall
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionapply(Statement base, Description description) Apply class-level features of the Spring TestContext Framework to the suppliedbasestatement.
-
Constructor Details
-
SpringClassRule
public SpringClassRule()
-
-
Method Details
-
apply
Apply class-level features of the Spring TestContext Framework to the suppliedbasestatement.Specifically, this method retrieves the
TestContextManagerused by this rule and its associatedSpringMethodRuleand invokes thebeforeTestClass()andafterTestClass()methods on theTestContextManager.In addition, this method checks whether the test is enabled in the current execution environment. This prevents classes with a non-matching
@IfProfileValueannotation from running altogether, even skipping the execution ofbeforeTestClass()methods inTestExecutionListeners.- Specified by:
applyin interfaceTestRule- Parameters:
base- the baseStatementthat this rule should be applied todescription- aDescriptionof the current test execution- Returns:
- a statement that wraps the supplied
basewith class-level features of the Spring TestContext Framework - See Also:
-
getTestContextManager(java.lang.Class<?>)withBeforeTestClassCallbacks(org.junit.runners.model.Statement, org.springframework.test.context.TestContextManager)withAfterTestClassCallbacks(org.junit.runners.model.Statement, org.springframework.test.context.TestContextManager)withProfileValueCheck(org.junit.runners.model.Statement, java.lang.Class<?>)withTestContextManagerCacheEviction(org.junit.runners.model.Statement, java.lang.Class<?>)
-