Class SpringJUnit4ClassRunner
- All Implemented Interfaces:
Describable
,Filterable
,Orderable
,Sortable
- Direct Known Subclasses:
SpringRunner
SpringJUnit4ClassRunner
is a custom extension of JUnit's
BlockJUnit4ClassRunner
which provides functionality of the
Spring TestContext Framework to standard JUnit tests by means of the
TestContextManager
and associated support classes and annotations.
To use this class, annotate a JUnit 4 based test class with
@RunWith(SpringJUnit4ClassRunner.class)
or @RunWith(SpringRunner.class)
.
The following list constitutes all annotations currently supported directly
or indirectly by SpringJUnit4ClassRunner
. (Note that additional
annotations may be supported by various
TestExecutionListener
or TestContextBootstrapper
implementations.)
@Test(expected=...)
@Test(timeout=...)
@Timed
@Repeat
@Ignore
@ProfileValueSourceConfiguration
@IfProfileValue
If you would like to use the Spring TestContext Framework with a runner
other than this one, use SpringClassRule
and SpringMethodRule
.
NOTE: This class requires JUnit 4.12 or higher.
- Since:
- 2.5
- Author:
- Sam Brannen, Juergen Hoeller
- See Also:
-
Constructor Summary
ConstructorDescriptionSpringJUnit4ClassRunner
(Class<?> clazz) Construct a newSpringJUnit4ClassRunner
and initialize aTestContextManager
to provide Spring testing functionality to standard JUnit tests. -
Method Summary
Modifier and TypeMethodDescriptionprotected Object
Delegate to the parent implementation for creating the test instance and then allow theTestContextManager
to prepare the test instance before returning it.protected TestContextManager
createTestContextManager
(Class<?> clazz) Create a newTestContextManager
for the supplied test class.Return a description suitable for an ignored test class if the test is disabled via@IfProfileValue
at the class-level, and otherwise delegate to the parent implementation.getExpectedException
(FrameworkMethod frameworkMethod) Get theexception
that the supplied test method is expected to throw.protected long
getJUnitTimeout
(FrameworkMethod frameworkMethod) protected long
getSpringTimeout
(FrameworkMethod frameworkMethod) Retrieve the configured Spring-specifictimeout
from the@Timed
annotation on the supplied test method.protected final TestContextManager
Get theTestContextManager
associated with this runner.protected boolean
isTestMethodIgnored
(FrameworkMethod frameworkMethod) Returntrue
if@Ignore
is present for the supplied test method or if the test method is disabled via@IfProfileValue
.protected Statement
methodBlock
(FrameworkMethod frameworkMethod) Augment the default JUnit behavior with potential repeats of the entire execution chain.protected Statement
possiblyExpectingExceptions
(FrameworkMethod frameworkMethod, Object testInstance, Statement next) Perform the same logic asBlockJUnit4ClassRunner.possiblyExpectingExceptions(FrameworkMethod, Object, Statement)
except that the expected exception is retrieved usinggetExpectedException(FrameworkMethod)
.void
run
(RunNotifier notifier) Check whether the test is enabled in the current execution environment.protected void
runChild
(FrameworkMethod frameworkMethod, RunNotifier notifier) Perform the same logic asBlockJUnit4ClassRunner.runChild(FrameworkMethod, RunNotifier)
, except that tests are determined to be ignored byisTestMethodIgnored(FrameworkMethod)
.protected Statement
withAfterClasses
(Statement statement) Wrap theStatement
returned by the parent implementation with aRunAfterTestClassCallbacks
statement, thus preserving the default JUnit functionality while adding support for the Spring TestContext Framework.protected Statement
withAfters
(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) Wrap theStatement
returned by the parent implementation with aRunAfterTestMethodCallbacks
statement, thus preserving the default functionality while adding support for the Spring TestContext Framework.protected Statement
withAfterTestExecutionCallbacks
(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) Wrap the suppliedStatement
with aRunAfterTestExecutionCallbacks
statement, thus preserving the default functionality while adding support for the Spring TestContext Framework.protected Statement
withBeforeClasses
(Statement statement) Wrap theStatement
returned by the parent implementation with aRunBeforeTestClassCallbacks
statement, thus preserving the default JUnit functionality while adding support for the Spring TestContext Framework.protected Statement
withBefores
(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) Wrap theStatement
returned by the parent implementation with aRunBeforeTestMethodCallbacks
statement, thus preserving the default functionality while adding support for the Spring TestContext Framework.protected Statement
withBeforeTestExecutionCallbacks
(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) Wrap the suppliedStatement
with aRunBeforeTestExecutionCallbacks
statement, thus preserving the default functionality while adding support for the Spring TestContext Framework.protected Statement
withPotentialRepeat
(FrameworkMethod frameworkMethod, Object testInstance, Statement next) Wrap the suppliedStatement
with aSpringRepeat
statement.protected Statement
withPotentialTimeout
(FrameworkMethod frameworkMethod, Object testInstance, Statement next) Perform the same logic asBlockJUnit4ClassRunner.withPotentialTimeout(FrameworkMethod, Object, Statement)
but with additional support for Spring's@Timed
annotation.Methods inherited from class org.junit.runners.BlockJUnit4ClassRunner
collectInitializationErrors, computeTestMethods, createTest, describeChild, getChildren, getTestRules, isIgnored, methodInvoker, rules, testName, validateConstructor, validateFields, validateInstanceMethods, validateNoNonStaticInnerClass, validateOnlyOneConstructor, validateTestMethods, validateZeroArgConstructor
Methods inherited from class org.junit.runners.ParentRunner
childrenInvoker, classBlock, classRules, createTestClass, filter, getName, getRunnerAnnotations, getTestClass, order, runLeaf, setScheduler, sort, validatePublicVoidNoArgMethods, withInterruptIsolation
-
Constructor Details
-
SpringJUnit4ClassRunner
Construct a newSpringJUnit4ClassRunner
and initialize aTestContextManager
to provide Spring testing functionality to standard JUnit tests.- Parameters:
clazz
- the test class to be run- Throws:
InitializationError
- See Also:
-
-
Method Details
-
createTestContextManager
Create a newTestContextManager
for the supplied test class.Can be overridden by subclasses.
- Parameters:
clazz
- the test class to be managed
-
getTestContextManager
Get theTestContextManager
associated with this runner. -
getDescription
Return a description suitable for an ignored test class if the test is disabled via@IfProfileValue
at the class-level, and otherwise delegate to the parent implementation.- Specified by:
getDescription
in interfaceDescribable
- Overrides:
getDescription
in classParentRunner<FrameworkMethod>
- See Also:
-
run
Check 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 ofprepareTestInstance()
methods inTestExecutionListeners
.- Overrides:
run
in classParentRunner<FrameworkMethod>
- See Also:
-
withBeforeClasses
Wrap theStatement
returned by the parent implementation with aRunBeforeTestClassCallbacks
statement, thus preserving the default JUnit functionality while adding support for the Spring TestContext Framework.- Overrides:
withBeforeClasses
in classParentRunner<FrameworkMethod>
- See Also:
-
withAfterClasses
Wrap theStatement
returned by the parent implementation with aRunAfterTestClassCallbacks
statement, thus preserving the default JUnit functionality while adding support for the Spring TestContext Framework.- Overrides:
withAfterClasses
in classParentRunner<FrameworkMethod>
- See Also:
-
createTest
Delegate to the parent implementation for creating the test instance and then allow theTestContextManager
to prepare the test instance before returning it.- Overrides:
createTest
in classBlockJUnit4ClassRunner
- Throws:
Exception
- See Also:
-
runChild
Perform the same logic asBlockJUnit4ClassRunner.runChild(FrameworkMethod, RunNotifier)
, except that tests are determined to be ignored byisTestMethodIgnored(FrameworkMethod)
.- Overrides:
runChild
in classBlockJUnit4ClassRunner
-
methodBlock
Augment the default JUnit behavior with potential repeats of the entire execution chain.Furthermore, support for timeouts has been moved down the execution chain in order to include execution of
@Before
and@After
methods within the timed execution. Note that this differs from the default JUnit behavior of executing@Before
and@After
methods in the main thread while executing the actual test method in a separate thread. Thus, the net effect is that@Before
and@After
methods will be executed in the same thread as the test method. As a consequence, JUnit-specified timeouts will work fine in combination with Spring transactions. However, JUnit-specific timeouts still differ from Spring-specific timeouts in that the former execute in a separate thread while the latter execute in the main thread (like regular tests).- Overrides:
methodBlock
in classBlockJUnit4ClassRunner
- See Also:
-
BlockJUnit4ClassRunner.methodInvoker(FrameworkMethod, Object)
withBeforeTestExecutionCallbacks(FrameworkMethod, Object, Statement)
withAfterTestExecutionCallbacks(FrameworkMethod, Object, Statement)
possiblyExpectingExceptions(FrameworkMethod, Object, Statement)
withBefores(FrameworkMethod, Object, Statement)
withAfters(FrameworkMethod, Object, Statement)
withRulesReflectively(FrameworkMethod, Object, Statement)
withPotentialRepeat(FrameworkMethod, Object, Statement)
withPotentialTimeout(FrameworkMethod, Object, Statement)
-
isTestMethodIgnored
Returntrue
if@Ignore
is present for the supplied test method or if the test method is disabled via@IfProfileValue
. -
possiblyExpectingExceptions
protected Statement possiblyExpectingExceptions(FrameworkMethod frameworkMethod, Object testInstance, Statement next) Perform the same logic asBlockJUnit4ClassRunner.possiblyExpectingExceptions(FrameworkMethod, Object, Statement)
except that the expected exception is retrieved usinggetExpectedException(FrameworkMethod)
.- Overrides:
possiblyExpectingExceptions
in classBlockJUnit4ClassRunner
-
getExpectedException
@Nullable protected Class<? extends Throwable> getExpectedException(FrameworkMethod frameworkMethod) Get theexception
that the supplied test method is expected to throw.Supports JUnit's
@Test(expected=...)
annotation.Can be overridden by subclasses.
- Returns:
- the expected exception, or
null
if none was specified
-
withPotentialTimeout
protected Statement withPotentialTimeout(FrameworkMethod frameworkMethod, Object testInstance, Statement next) Perform the same logic asBlockJUnit4ClassRunner.withPotentialTimeout(FrameworkMethod, Object, Statement)
but with additional support for Spring's@Timed
annotation.Supports both Spring's
@Timed
and JUnit's@Test(timeout=...)
annotations, but not both simultaneously.- Overrides:
withPotentialTimeout
in classBlockJUnit4ClassRunner
- Returns:
- either a
SpringFailOnTimeout
, aFailOnTimeout
, or the suppliedStatement
as appropriate - See Also:
-
getJUnitTimeout
- Returns:
- the timeout, or
0
if none was specified
-
getSpringTimeout
Retrieve the configured Spring-specifictimeout
from the@Timed
annotation on the supplied test method.- Returns:
- the timeout, or
0
if none was specified - See Also:
-
withBeforeTestExecutionCallbacks
protected Statement withBeforeTestExecutionCallbacks(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) Wrap the suppliedStatement
with aRunBeforeTestExecutionCallbacks
statement, thus preserving the default functionality while adding support for the Spring TestContext Framework.- See Also:
-
withAfterTestExecutionCallbacks
protected Statement withAfterTestExecutionCallbacks(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) Wrap the suppliedStatement
with aRunAfterTestExecutionCallbacks
statement, thus preserving the default functionality while adding support for the Spring TestContext Framework.- See Also:
-
withBefores
protected Statement withBefores(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) Wrap theStatement
returned by the parent implementation with aRunBeforeTestMethodCallbacks
statement, thus preserving the default functionality while adding support for the Spring TestContext Framework.- Overrides:
withBefores
in classBlockJUnit4ClassRunner
- See Also:
-
withAfters
protected Statement withAfters(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) Wrap theStatement
returned by the parent implementation with aRunAfterTestMethodCallbacks
statement, thus preserving the default functionality while adding support for the Spring TestContext Framework.- Overrides:
withAfters
in classBlockJUnit4ClassRunner
- See Also:
-
withPotentialRepeat
protected Statement withPotentialRepeat(FrameworkMethod frameworkMethod, Object testInstance, Statement next)
-