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 SummaryConstructorsConstructorDescriptionSpringJUnit4ClassRunner(Class<?> clazz) Deprecated.Construct a newSpringJUnit4ClassRunnerand initialize aTestContextManagerto provide Spring testing functionality to standard JUnit tests.
- 
Method SummaryModifier and TypeMethodDescriptionprotected ObjectDeprecated.Delegate to the parent implementation for creating the test instance and then allow theTestContextManagerto prepare the test instance before returning it.protected TestContextManagercreateTestContextManager(Class<?> clazz) Deprecated.Create a newTestContextManagerfor the supplied test class.Deprecated.Return a description suitable for an ignored test class if the test is disabled via@IfProfileValueat the class-level, and otherwise delegate to the parent implementation.getExpectedException(FrameworkMethod frameworkMethod) Deprecated.Get theexceptionthat the supplied test method is expected to throw.protected longgetJUnitTimeout(FrameworkMethod frameworkMethod) Deprecated.protected longgetSpringTimeout(FrameworkMethod frameworkMethod) Deprecated.Retrieve the configured Spring-specifictimeoutfrom the@Timedannotation on the supplied test method.protected final TestContextManagerDeprecated.Get theTestContextManagerassociated with this runner.protected booleanisTestMethodIgnored(FrameworkMethod frameworkMethod) Deprecated.Returntrueif@Ignoreis present for the supplied test method or if the test method is disabled via@IfProfileValue.protected StatementmethodBlock(FrameworkMethod frameworkMethod) Deprecated.Augment the default JUnit behavior with potential repeats of the entire execution chain.protected StatementpossiblyExpectingExceptions(FrameworkMethod frameworkMethod, Object testInstance, Statement next) Deprecated.Perform the same logic asBlockJUnit4ClassRunner.possiblyExpectingExceptions(FrameworkMethod, Object, Statement)except that the expected exception is retrieved usinggetExpectedException(FrameworkMethod).voidrun(RunNotifier notifier) Deprecated.Check whether the test is enabled in the current execution environment.protected voidrunChild(FrameworkMethod frameworkMethod, RunNotifier notifier) Deprecated.Perform the same logic asBlockJUnit4ClassRunner.runChild(FrameworkMethod, RunNotifier), except that tests are determined to be ignored byisTestMethodIgnored(FrameworkMethod).protected StatementwithAfterClasses(Statement statement) Deprecated.Wrap theStatementreturned by the parent implementation with aRunAfterTestClassCallbacksstatement, thus preserving the default JUnit functionality while adding support for the Spring TestContext Framework.protected StatementwithAfters(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) Deprecated.Wrap theStatementreturned by the parent implementation with aRunAfterTestMethodCallbacksstatement, thus preserving the default functionality while adding support for the Spring TestContext Framework.protected StatementwithAfterTestExecutionCallbacks(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) Deprecated.Wrap the suppliedStatementwith aRunAfterTestExecutionCallbacksstatement, thus preserving the default functionality while adding support for the Spring TestContext Framework.protected StatementwithBeforeClasses(Statement statement) Deprecated.Wrap theStatementreturned by the parent implementation with aRunBeforeTestClassCallbacksstatement, thus preserving the default JUnit functionality while adding support for the Spring TestContext Framework.protected StatementwithBefores(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) Deprecated.Wrap theStatementreturned by the parent implementation with aRunBeforeTestMethodCallbacksstatement, thus preserving the default functionality while adding support for the Spring TestContext Framework.protected StatementwithBeforeTestExecutionCallbacks(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) Deprecated.Wrap the suppliedStatementwith aRunBeforeTestExecutionCallbacksstatement, thus preserving the default functionality while adding support for the Spring TestContext Framework.protected StatementwithPotentialRepeat(FrameworkMethod frameworkMethod, Object testInstance, Statement next) Deprecated.Wrap the suppliedStatementwith aSpringRepeatstatement.protected StatementwithPotentialTimeout(FrameworkMethod frameworkMethod, Object testInstance, Statement next) Deprecated.Perform the same logic asBlockJUnit4ClassRunner.withPotentialTimeout(FrameworkMethod, Object, Statement)but with additional support for Spring's@Timedannotation.Methods inherited from class org.junit.runners.BlockJUnit4ClassRunnercollectInitializationErrors, computeTestMethods, createTest, describeChild, getChildren, getTestRules, isIgnored, methodInvoker, rules, testName, validateConstructor, validateFields, validateInstanceMethods, validateNoNonStaticInnerClass, validateOnlyOneConstructor, validateTestMethods, validateZeroArgConstructorMethods inherited from class org.junit.runners.ParentRunnerchildrenInvoker, classBlock, classRules, createTestClass, filter, getName, getRunnerAnnotations, getTestClass, order, runLeaf, setScheduler, sort, validatePublicVoidNoArgMethods, withInterruptIsolation
- 
Constructor Details- 
SpringJUnit4ClassRunnerDeprecated.Construct a newSpringJUnit4ClassRunnerand initialize aTestContextManagerto provide Spring testing functionality to standard JUnit tests.- Parameters:
- clazz- the test class to be run
- Throws:
- InitializationError
- See Also:
 
 
- 
- 
Method Details- 
createTestContextManagerDeprecated.Create a newTestContextManagerfor the supplied test class.Can be overridden by subclasses. - Parameters:
- clazz- the test class to be managed
 
- 
getTestContextManagerDeprecated.Get theTestContextManagerassociated with this runner.
- 
getDescriptionDeprecated.Return a description suitable for an ignored test class if the test is disabled via@IfProfileValueat the class-level, and otherwise delegate to the parent implementation.- Specified by:
- getDescriptionin interface- Describable
- Overrides:
- getDescriptionin class- ParentRunner<FrameworkMethod>
- See Also:
 
- 
runDeprecated.Check 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 ofprepareTestInstance()methods inTestExecutionListeners.- Overrides:
- runin class- ParentRunner<FrameworkMethod>
- See Also:
 
- 
withBeforeClassesDeprecated.Wrap theStatementreturned by the parent implementation with aRunBeforeTestClassCallbacksstatement, thus preserving the default JUnit functionality while adding support for the Spring TestContext Framework.- Overrides:
- withBeforeClassesin class- ParentRunner<FrameworkMethod>
- See Also:
 
- 
withAfterClassesDeprecated.Wrap theStatementreturned by the parent implementation with aRunAfterTestClassCallbacksstatement, thus preserving the default JUnit functionality while adding support for the Spring TestContext Framework.- Overrides:
- withAfterClassesin class- ParentRunner<FrameworkMethod>
- See Also:
 
- 
createTestDeprecated.Delegate to the parent implementation for creating the test instance and then allow theTestContextManagerto prepare the test instance before returning it.- Overrides:
- createTestin class- BlockJUnit4ClassRunner
- Throws:
- Exception
- See Also:
 
- 
runChildDeprecated.Perform the same logic asBlockJUnit4ClassRunner.runChild(FrameworkMethod, RunNotifier), except that tests are determined to be ignored byisTestMethodIgnored(FrameworkMethod).- Overrides:
- runChildin class- BlockJUnit4ClassRunner
 
- 
methodBlockDeprecated.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 @Beforeand@Aftermethods within the timed execution. Note that this differs from the default JUnit behavior of executing@Beforeand@Aftermethods in the main thread while executing the actual test method in a separate thread. Thus, the net effect is that@Beforeand@Aftermethods 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:
- methodBlockin class- BlockJUnit4ClassRunner
- See Also:
 
- 
isTestMethodIgnoredDeprecated.Returntrueif@Ignoreis present for the supplied test method or if the test method is disabled via@IfProfileValue.- See Also:
 
- 
possiblyExpectingExceptionsprotected Statement possiblyExpectingExceptions(FrameworkMethod frameworkMethod, Object testInstance, Statement next) Deprecated.Perform the same logic asBlockJUnit4ClassRunner.possiblyExpectingExceptions(FrameworkMethod, Object, Statement)except that the expected exception is retrieved usinggetExpectedException(FrameworkMethod).- Overrides:
- possiblyExpectingExceptionsin class- BlockJUnit4ClassRunner
 
- 
getExpectedExceptionprotected @Nullable Class<? extends Throwable> getExpectedException(FrameworkMethod frameworkMethod) Deprecated.Get theexceptionthat the supplied test method is expected to throw.Supports JUnit's @Test(expected=...)annotation.Can be overridden by subclasses. - Returns:
- the expected exception, or nullif none was specified
 
- 
withPotentialTimeoutprotected Statement withPotentialTimeout(FrameworkMethod frameworkMethod, Object testInstance, Statement next) Deprecated.Perform the same logic asBlockJUnit4ClassRunner.withPotentialTimeout(FrameworkMethod, Object, Statement)but with additional support for Spring's@Timedannotation.Supports both Spring's @Timedand JUnit's@Test(timeout=...)annotations, but not both simultaneously.- Overrides:
- withPotentialTimeoutin class- BlockJUnit4ClassRunner
- Returns:
- either a SpringFailOnTimeout, aFailOnTimeout, or the suppliedStatementas appropriate
- See Also:
 
- 
getJUnitTimeoutDeprecated.- Returns:
- the timeout, or 0if none was specified
 
- 
getSpringTimeoutDeprecated.Retrieve the configured Spring-specifictimeoutfrom the@Timedannotation on the supplied test method.- Returns:
- the timeout, or 0if none was specified
- See Also:
 
- 
withBeforeTestExecutionCallbacksprotected Statement withBeforeTestExecutionCallbacks(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) Deprecated.Wrap the suppliedStatementwith aRunBeforeTestExecutionCallbacksstatement, thus preserving the default functionality while adding support for the Spring TestContext Framework.- See Also:
 
- 
withAfterTestExecutionCallbacksprotected Statement withAfterTestExecutionCallbacks(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) Deprecated.Wrap the suppliedStatementwith aRunAfterTestExecutionCallbacksstatement, thus preserving the default functionality while adding support for the Spring TestContext Framework.- See Also:
 
- 
withBeforesprotected Statement withBefores(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) Deprecated.Wrap theStatementreturned by the parent implementation with aRunBeforeTestMethodCallbacksstatement, thus preserving the default functionality while adding support for the Spring TestContext Framework.- Overrides:
- withBeforesin class- BlockJUnit4ClassRunner
- See Also:
 
- 
withAftersprotected Statement withAfters(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) Deprecated.Wrap theStatementreturned by the parent implementation with aRunAfterTestMethodCallbacksstatement, thus preserving the default functionality while adding support for the Spring TestContext Framework.- Overrides:
- withAftersin class- BlockJUnit4ClassRunner
- See Also:
 
- 
withPotentialRepeat
 
- 
SpringExtensionand JUnit Jupiter