public class SpringClassRule
extends java.lang.Object
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.
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: As of Spring Framework 4.3, this class requires JUnit 4.12 or higher.
apply(Statement, Description),
SpringMethodRule,
TestContextManager,
SpringJUnit4ClassRunner| Modifier and Type | Class and Description |
|---|---|
private static class |
SpringClassRule.TestContextManagerCacheEvictor |
| Modifier and Type | Field and Description |
|---|---|
private static Log |
logger |
private static java.util.Map<java.lang.Class<?>,TestContextManager> |
testContextManagerCache
Cache of
TestContextManagers keyed by test class. |
| Constructor and Description |
|---|
SpringClassRule() |
| Modifier and Type | Method and Description |
|---|---|
Statement |
apply(Statement base,
Description description)
Apply class-level features of the Spring TestContext
Framework to the supplied
base statement. |
private static java.util.Optional<java.lang.reflect.Field> |
findSpringMethodRuleField(java.lang.Class<?> testClass) |
(package private) static TestContextManager |
getTestContextManager(java.lang.Class<?> testClass)
Get the
TestContextManager associated with the supplied test class. |
private static void |
validateSpringMethodRuleConfiguration(java.lang.Class<?> testClass)
Throw an
IllegalStateException if the supplied testClass
does not declare a public SpringMethodRule field that is
annotated with @Rule. |
private Statement |
withAfterTestClassCallbacks(Statement next,
TestContextManager testContextManager)
Wrap the supplied
Statement with a RunAfterTestClassCallbacks statement. |
private Statement |
withBeforeTestClassCallbacks(Statement next,
TestContextManager testContextManager)
Wrap the supplied
Statement with a RunBeforeTestClassCallbacks statement. |
private Statement |
withProfileValueCheck(Statement next,
java.lang.Class<?> testClass)
Wrap the supplied
Statement with a ProfileValueChecker statement. |
private Statement |
withTestContextManagerCacheEviction(Statement next,
java.lang.Class<?> testClass)
Wrap the supplied
Statement with a TestContextManagerCacheEvictor statement. |
private static final Log logger
private static final java.util.Map<java.lang.Class<?>,TestContextManager> testContextManagerCache
TestContextManagers keyed by test class.public Statement apply(Statement base,
Description description)
base statement.
Specifically, this method retrieves the TestContextManager
used by this rule and its associated SpringMethodRule and
invokes the beforeTestClass()
and afterTestClass() methods
on the TestContextManager.
In addition, this method checks whether the test is enabled in
the current execution environment. This prevents classes with a
non-matching @IfProfileValue annotation from running altogether,
even skipping the execution of beforeTestClass() methods
in TestExecutionListeners.
base - the base Statement that this rule should be applied todescription - a Description of the current test executionbase with class-level
features of the Spring TestContext FrameworkgetTestContextManager(java.lang.Class<?>),
withBeforeTestClassCallbacks(Statement, org.springframework.test.context.TestContextManager),
withAfterTestClassCallbacks(Statement, org.springframework.test.context.TestContextManager),
withProfileValueCheck(Statement, java.lang.Class<?>),
withTestContextManagerCacheEviction(Statement, java.lang.Class<?>)private Statement withBeforeTestClassCallbacks(Statement next,
TestContextManager testContextManager)
Statement with a RunBeforeTestClassCallbacks statement.RunBeforeTestClassCallbacksprivate Statement withAfterTestClassCallbacks(Statement next,
TestContextManager testContextManager)
Statement with a RunAfterTestClassCallbacks statement.RunAfterTestClassCallbacksprivate Statement withProfileValueCheck(Statement next,
java.lang.Class<?> testClass)
Statement with a ProfileValueChecker statement.ProfileValueCheckerprivate Statement withTestContextManagerCacheEviction(Statement next,
java.lang.Class<?> testClass)
Statement with a TestContextManagerCacheEvictor statement.private static void validateSpringMethodRuleConfiguration(java.lang.Class<?> testClass)
IllegalStateException if the supplied testClass
does not declare a public SpringMethodRule field that is
annotated with @Rule.private static java.util.Optional<java.lang.reflect.Field> findSpringMethodRuleField(java.lang.Class<?> testClass)
static TestContextManager getTestContextManager(java.lang.Class<?> testClass)
TestContextManager associated with the supplied test class.testClass - the test class to be managed; never null