public class

SpringJUnit4ClassRunner

extends BlockJUnit4ClassRunner
java.lang.Object
   ↳ BlockJUnit4ClassRunner
     ↳ org.springframework.test.context.junit4.SpringJUnit4ClassRunner

Class Overview

SpringJUnit4ClassRunner is a custom extension of BlockJUnit4ClassRunner which provides functionality of the Spring TestContext Framework to standard JUnit 4.5+ tests by means of the TestContextManager and associated support classes and annotations.

The following list constitutes all annotations currently supported directly by SpringJUnit4ClassRunner. (Note that additional annotations may be supported by various TestExecutionListeners)

NOTE: As of Spring 3.0, SpringJUnit4ClassRunner requires JUnit 4.5+.

Summary

Public Constructors
SpringJUnit4ClassRunner(Class<?> clazz)
Constructs a new SpringJUnit4ClassRunner and initializes a TestContextManager to provide Spring testing functionality to standard JUnit tests.
Public Methods
Description getDescription()
Returns a description suitable for an ignored test class if the test is disabled via @IfProfileValue at the class-level, and otherwise delegates to the parent implementation.
void run(RunNotifier notifier)
Check whether the test is enabled in the first place.
Protected Methods
Object createTest()
Delegates to the parent implementation for creating the test instance and then allows the TestContextManager to prepare the test instance before returning it.
TestContextManager createTestContextManager(Class<?> clazz)
Creates a new TestContextManager for the supplied test class and the configured default ContextLoader class name.
String getDefaultContextLoaderClassName(Class<?> clazz)
Get the name of the default ContextLoader class to use for the supplied test class.
Class<? extends Throwable> getExpectedException(FrameworkMethod frameworkMethod)
Get the exception that the supplied FrameworkMethod test method is expected to throw.
long getJUnitTimeout(FrameworkMethod frameworkMethod)
Retrieves the configured JUnit timeout from the Test @Test annotation on the supplied FrameworkMethod test method.
long getSpringTimeout(FrameworkMethod frameworkMethod)
Retrieves the configured Spring-specific timeout from the @Timed annotation on the supplied FrameworkMethod test method.
final TestContextManager getTestContextManager()
Get the TestContextManager associated with this runner.
boolean isTestMethodIgnored(FrameworkMethod frameworkMethod)
Returns true if Ignore @Ignore is present for the supplied FrameworkMethod test method or if the test method is disabled via @IfProfileValue.
Statement methodBlock(FrameworkMethod frameworkMethod)
Augments the default JUnit behavior with potential repeats of the entire execution chain.
Statement possiblyExpectingExceptions(FrameworkMethod frameworkMethod, Object testInstance, Statement next)
Performs the same logic as BlockJUnit4ClassRunner#possiblyExpectingExceptions(FrameworkMethod, Object, Statement) except that the expected exception is retrieved using getExpectedException(FrameworkMethod).
void runChild(FrameworkMethod frameworkMethod, RunNotifier notifier)
Performs the same logic as BlockJUnit4ClassRunner#runChild(FrameworkMethod, RunNotifier), except that tests are determined to be ignored by isTestMethodIgnored(FrameworkMethod).
Statement withAfterClasses(Statement statement)
Wraps the Statement returned by the parent implementation with a RunAfterTestClassCallbacks statement, thus preserving the default functionality but adding support for the Spring TestContext Framework.
Statement withAfters(FrameworkMethod frameworkMethod, Object testInstance, Statement statement)
Wraps the Statement returned by the parent implementation with a RunAfterTestMethodCallbacks statement, thus preserving the default functionality but adding support for the Spring TestContext Framework.
Statement withBeforeClasses(Statement statement)
Wraps the Statement returned by the parent implementation with a RunBeforeTestClassCallbacks statement, thus preserving the default functionality but adding support for the Spring TestContext Framework.
Statement withBefores(FrameworkMethod frameworkMethod, Object testInstance, Statement statement)
Wraps the Statement returned by the parent implementation with a RunBeforeTestMethodCallbacks statement, thus preserving the default functionality but adding support for the Spring TestContext Framework.
Statement withPotentialRepeat(FrameworkMethod frameworkMethod, Object testInstance, Statement next)
Supports Spring's @Repeat annotation by returning a SpringRepeat statement initialized with the configured repeat count or 1 if no repeat count is configured.
Statement withPotentialTimeout(FrameworkMethod frameworkMethod, Object testInstance, Statement next)
Supports both Spring's @Timed and JUnit's Test#timeout() @Test(timeout=...) annotations, but not both simultaneously.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public SpringJUnit4ClassRunner (Class<?> clazz)

Constructs a new SpringJUnit4ClassRunner and initializes a TestContextManager to provide Spring testing functionality to standard JUnit tests.

Parameters
clazz the test class to be run
Throws
InitializationError

Public Methods

public Description getDescription ()

Returns a description suitable for an ignored test class if the test is disabled via @IfProfileValue at the class-level, and otherwise delegates to the parent implementation.

public void run (RunNotifier notifier)

Check whether the test is enabled in the first place. This prevents classes with a non-matching @IfProfileValue annotation from running altogether, even skipping the execution of prepareTestInstance() TestExecutionListener methods.

Protected Methods

protected Object createTest ()

Delegates to the parent implementation for creating the test instance and then allows the TestContextManager to prepare the test instance before returning it.

Throws
Exception

protected TestContextManager createTestContextManager (Class<?> clazz)

Creates a new TestContextManager for the supplied test class and the configured default ContextLoader class name. Can be overridden by subclasses.

Parameters
clazz the test class to be managed

protected String getDefaultContextLoaderClassName (Class<?> clazz)

Get the name of the default ContextLoader class to use for the supplied test class. The named class will be used if the test class does not explicitly declare a ContextLoader class via the @ContextConfiguration annotation.

The default implementation returns null, thus implying use of the standard default ContextLoader class name. Can be overridden by subclasses.

Parameters
clazz the test class
Returns
  • null

protected Class<? extends Throwable> getExpectedException (FrameworkMethod frameworkMethod)

Get the exception that the supplied FrameworkMethod test method is expected to throw.

Supports both Spring's @ExpectedException(...) and JUnit's Test#expected() @Test(expected=...) annotations, but not both simultaneously.

Returns
  • the expected exception, or null if none was specified

protected long getJUnitTimeout (FrameworkMethod frameworkMethod)

Retrieves the configured JUnit timeout from the Test @Test annotation on the supplied FrameworkMethod test method.

Returns
  • the timeout, or 0 if none was specified.

protected long getSpringTimeout (FrameworkMethod frameworkMethod)

Retrieves the configured Spring-specific timeout from the @Timed annotation on the supplied FrameworkMethod test method.

Returns
  • the timeout, or 0 if none was specified.

protected final TestContextManager getTestContextManager ()

Get the TestContextManager associated with this runner.

protected boolean isTestMethodIgnored (FrameworkMethod frameworkMethod)

Returns true if Ignore @Ignore is present for the supplied FrameworkMethod test method or if the test method is disabled via @IfProfileValue.

protected Statement methodBlock (FrameworkMethod frameworkMethod)

Augments 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 org.junit.Before @Before and org.junit.After @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 end 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. Note that 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).

protected Statement possiblyExpectingExceptions (FrameworkMethod frameworkMethod, Object testInstance, Statement next)

Performs the same logic as BlockJUnit4ClassRunner#possiblyExpectingExceptions(FrameworkMethod, Object, Statement) except that the expected exception is retrieved using getExpectedException(FrameworkMethod).

protected void runChild (FrameworkMethod frameworkMethod, RunNotifier notifier)

Performs the same logic as BlockJUnit4ClassRunner#runChild(FrameworkMethod, RunNotifier), except that tests are determined to be ignored by isTestMethodIgnored(FrameworkMethod).

protected Statement withAfterClasses (Statement statement)

Wraps the Statement returned by the parent implementation with a RunAfterTestClassCallbacks statement, thus preserving the default functionality but adding support for the Spring TestContext Framework.

protected Statement withAfters (FrameworkMethod frameworkMethod, Object testInstance, Statement statement)

Wraps the Statement returned by the parent implementation with a RunAfterTestMethodCallbacks statement, thus preserving the default functionality but adding support for the Spring TestContext Framework.

protected Statement withBeforeClasses (Statement statement)

Wraps the Statement returned by the parent implementation with a RunBeforeTestClassCallbacks statement, thus preserving the default functionality but adding support for the Spring TestContext Framework.

protected Statement withBefores (FrameworkMethod frameworkMethod, Object testInstance, Statement statement)

Wraps the Statement returned by the parent implementation with a RunBeforeTestMethodCallbacks statement, thus preserving the default functionality but adding support for the Spring TestContext Framework.

protected Statement withPotentialRepeat (FrameworkMethod frameworkMethod, Object testInstance, Statement next)

Supports Spring's @Repeat annotation by returning a SpringRepeat statement initialized with the configured repeat count or 1 if no repeat count is configured.

See Also

protected Statement withPotentialTimeout (FrameworkMethod frameworkMethod, Object testInstance, Statement next)

Supports both Spring's @Timed and JUnit's Test#timeout() @Test(timeout=...) annotations, but not both simultaneously. Returns either a SpringFailOnTimeout, a FailOnTimeout, or the unmodified, supplied Statement as appropriate.