public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner
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, simply 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: As of Spring Framework 4.3, this class requires JUnit 4.12 or higher.
SpringRunner
,
TestContextManager
,
AbstractJUnit4SpringContextTests
,
AbstractTransactionalJUnit4SpringContextTests
,
SpringClassRule
,
SpringMethodRule
Constructor and Description |
---|
SpringJUnit4ClassRunner(Class<?> clazz)
Construct a new
SpringJUnit4ClassRunner and initialize a
TestContextManager to provide Spring testing functionality to
standard JUnit tests. |
Modifier and Type | Method and Description |
---|---|
protected Object |
createTest()
Delegate to the parent implementation for creating the test instance and
then allow the
TestContextManager to
prepare the test instance before returning it. |
protected TestContextManager |
createTestContextManager(Class<?> clazz)
Create a new
TestContextManager for the supplied test class. |
Description |
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. |
protected Class<? extends Throwable> |
getExpectedException(FrameworkMethod frameworkMethod)
Get the
exception that the supplied test method is expected to throw. |
protected long |
getJUnitTimeout(FrameworkMethod frameworkMethod)
|
protected long |
getSpringTimeout(FrameworkMethod frameworkMethod)
Retrieve the configured Spring-specific
timeout from the
@Timed annotation
on the supplied test method. |
protected TestContextManager |
getTestContextManager()
Get the
TestContextManager associated with this runner. |
protected boolean |
isTestMethodIgnored(FrameworkMethod frameworkMethod)
Return
true 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 as
BlockJUnit4ClassRunner.possiblyExpectingExceptions(FrameworkMethod, Object, Statement)
except that the expected exception is retrieved using
getExpectedException(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 as
BlockJUnit4ClassRunner.runChild(FrameworkMethod, RunNotifier) ,
except that tests are determined to be ignored by
isTestMethodIgnored(FrameworkMethod) . |
protected Statement |
withAfterClasses(Statement statement)
Wrap the
Statement returned by the parent implementation with a
RunAfterTestClassCallbacks 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 the
Statement returned by the parent implementation with a
RunAfterTestMethodCallbacks 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 supplied
Statement with a RunAfterTestExecutionCallbacks
statement, thus preserving the default functionality while adding support for the
Spring TestContext Framework. |
protected Statement |
withBeforeClasses(Statement statement)
Wrap the
Statement returned by the parent implementation with a
RunBeforeTestClassCallbacks 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 the
Statement returned by the parent implementation with a
RunBeforeTestMethodCallbacks 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 supplied
Statement with a RunBeforeTestExecutionCallbacks
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 supplied
Statement with a SpringRepeat statement. |
protected Statement |
withPotentialTimeout(FrameworkMethod frameworkMethod,
Object testInstance,
Statement next)
Perform the same logic as
BlockJUnit4ClassRunner.withPotentialTimeout(FrameworkMethod, Object, Statement)
but with additional support for Spring's @Timed annotation. |
collectInitializationErrors, computeTestMethods, describeChild, getChildren, getTestRules, isIgnored, methodInvoker, rules, testName, validateConstructor, validateFields, validateInstanceMethods, validateNoNonStaticInnerClass, validateOnlyOneConstructor, validateTestMethods, validateZeroArgConstructor
childrenInvoker, classBlock, classRules, createTestClass, filter, getName, getRunnerAnnotations, getTestClass, runLeaf, setScheduler, sort, validatePublicVoidNoArgMethods
public SpringJUnit4ClassRunner(Class<?> clazz) throws InitializationError
SpringJUnit4ClassRunner
and initialize a
TestContextManager
to provide Spring testing functionality to
standard JUnit tests.clazz
- the test class to be runInitializationError
createTestContextManager(Class)
protected TestContextManager createTestContextManager(Class<?> clazz)
TestContextManager
for the supplied test class.
Can be overridden by subclasses.
clazz
- the test class to be managedprotected final TestContextManager getTestContextManager()
TestContextManager
associated with this runner.public Description getDescription()
@IfProfileValue
at the class-level, and
otherwise delegate to the parent implementation.getDescription
in interface Describable
getDescription
in class ParentRunner<FrameworkMethod>
ProfileValueUtils.isTestEnabledInThisEnvironment(Class)
public void run(RunNotifier notifier)
This prevents classes with a non-matching @IfProfileValue
annotation from running altogether, even skipping the execution of
prepareTestInstance()
methods in TestExecutionListeners
.
run
in class ParentRunner<FrameworkMethod>
ProfileValueUtils.isTestEnabledInThisEnvironment(Class)
,
IfProfileValue
,
TestExecutionListener
protected Statement withBeforeClasses(Statement statement)
Statement
returned by the parent implementation with a
RunBeforeTestClassCallbacks
statement, thus preserving the
default JUnit functionality while adding support for the Spring TestContext
Framework.withBeforeClasses
in class ParentRunner<FrameworkMethod>
RunBeforeTestClassCallbacks
protected Statement withAfterClasses(Statement statement)
Statement
returned by the parent implementation with a
RunAfterTestClassCallbacks
statement, thus preserving the default
JUnit functionality while adding support for the Spring TestContext Framework.withAfterClasses
in class ParentRunner<FrameworkMethod>
RunAfterTestClassCallbacks
protected Object createTest() throws Exception
TestContextManager
to
prepare the test instance before returning it.createTest
in class BlockJUnit4ClassRunner
Exception
TestContextManager.prepareTestInstance(java.lang.Object)
protected void runChild(FrameworkMethod frameworkMethod, RunNotifier notifier)
BlockJUnit4ClassRunner.runChild(FrameworkMethod, RunNotifier)
,
except that tests are determined to be ignored by
isTestMethodIgnored(FrameworkMethod)
.runChild
in class BlockJUnit4ClassRunner
protected Statement methodBlock(FrameworkMethod frameworkMethod)
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 simply execute in the main thread (like regular
tests).
methodBlock
in class BlockJUnit4ClassRunner
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)
protected boolean isTestMethodIgnored(FrameworkMethod frameworkMethod)
true
if @Ignore
is present for the supplied
test method or if the test method is disabled
via @IfProfileValue
.protected Statement possiblyExpectingExceptions(FrameworkMethod frameworkMethod, Object testInstance, Statement next)
BlockJUnit4ClassRunner.possiblyExpectingExceptions(FrameworkMethod, Object, Statement)
except that the expected exception is retrieved using
getExpectedException(FrameworkMethod)
.possiblyExpectingExceptions
in class BlockJUnit4ClassRunner
@Nullable protected Class<? extends Throwable> getExpectedException(FrameworkMethod frameworkMethod)
exception
that the supplied test method is expected to throw.
Supports JUnit's @Test(expected=...)
annotation.
Can be overridden by subclasses.
null
if none was specifiedprotected Statement withPotentialTimeout(FrameworkMethod frameworkMethod, Object testInstance, Statement next)
BlockJUnit4ClassRunner.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.
withPotentialTimeout
in class BlockJUnit4ClassRunner
SpringFailOnTimeout
, a FailOnTimeout
,
or the supplied Statement
as appropriategetSpringTimeout(FrameworkMethod)
,
getJUnitTimeout(FrameworkMethod)
protected long getJUnitTimeout(FrameworkMethod frameworkMethod)
0
if none was specifiedprotected long getSpringTimeout(FrameworkMethod frameworkMethod)
timeout
from the
@Timed
annotation
on the supplied test method.0
if none was specifiedTestAnnotationUtils.getTimeout(Method)
protected Statement withBeforeTestExecutionCallbacks(FrameworkMethod frameworkMethod, Object testInstance, Statement statement)
Statement
with a RunBeforeTestExecutionCallbacks
statement, thus preserving the default functionality while adding support for the
Spring TestContext Framework.RunBeforeTestExecutionCallbacks
protected Statement withAfterTestExecutionCallbacks(FrameworkMethod frameworkMethod, Object testInstance, Statement statement)
Statement
with a RunAfterTestExecutionCallbacks
statement, thus preserving the default functionality while adding support for the
Spring TestContext Framework.RunAfterTestExecutionCallbacks
protected Statement withBefores(FrameworkMethod frameworkMethod, Object testInstance, Statement statement)
Statement
returned by the parent implementation with a
RunBeforeTestMethodCallbacks
statement, thus preserving the
default functionality while adding support for the Spring TestContext
Framework.withBefores
in class BlockJUnit4ClassRunner
RunBeforeTestMethodCallbacks
protected Statement withAfters(FrameworkMethod frameworkMethod, Object testInstance, Statement statement)
Statement
returned by the parent implementation with a
RunAfterTestMethodCallbacks
statement, thus preserving the
default functionality while adding support for the Spring TestContext
Framework.withAfters
in class BlockJUnit4ClassRunner
RunAfterTestMethodCallbacks
protected Statement withPotentialRepeat(FrameworkMethod frameworkMethod, Object testInstance, Statement next)