public class TestContextManager
extends java.lang.Object
TestContextManager
is the main entry point into the Spring
TestContext Framework.
Specifically, a TestContextManager
is responsible for managing a
single TestContext
and signaling events to all registered
TestExecutionListeners
at the following test
execution points:
before test class execution
: prior to any
before class callbacks of a particular testing framework (e.g.,
JUnit 4's @BeforeClass
)test instance preparation
:
immediately following instantiation of the test classbefore test setup
:
prior to any before method callbacks of a particular testing framework
(e.g., JUnit 4's @Before
)before test execution
:
immediately before execution of the test method but after test setupafter test execution
:
immediately after execution of the test method but before test tear downafter test tear down
:
after any after method callbacks of a particular testing
framework (e.g., JUnit 4's @After
)after test class execution
: after any
after class callbacks of a particular testing framework (e.g., JUnit 4's
@AfterClass
)Support for loading and accessing
application contexts,
dependency injection of test instances,
transactional
execution of test methods, etc. is provided by
ContextLoaders
and TestExecutionListeners
, which are configured via
@ContextConfiguration
and
@TestExecutionListeners
.
Bootstrapping of the TestContext
, the default ContextLoader
,
default TestExecutionListeners
, and their collaborators is performed
by a TestContextBootstrapper
, which is configured via
@BootstrapWith
.
BootstrapWith
,
BootstrapContext
,
TestContextBootstrapper
,
TestContext
,
TestExecutionListener
,
TestExecutionListeners
,
ContextConfiguration
,
ContextHierarchy
Constructor and Description |
---|
TestContextManager(java.lang.Class<?> testClass)
Construct a new
TestContextManager for the supplied test class. |
TestContextManager(TestContextBootstrapper testContextBootstrapper)
Construct a new
TestContextManager using the supplied TestContextBootstrapper
and register the necessary
TestExecutionListeners . |
Modifier and Type | Method and Description |
---|---|
void |
afterTestClass()
Hook for post-processing a test class after execution of all
tests within the class.
|
void |
afterTestExecution(java.lang.Object testInstance,
java.lang.reflect.Method testMethod,
java.lang.Throwable exception)
Hook for post-processing a test immediately after execution of
the test method in the supplied
test context — for example, for timing
or logging purposes.
|
void |
afterTestMethod(java.lang.Object testInstance,
java.lang.reflect.Method testMethod,
java.lang.Throwable exception)
Hook for post-processing a test after execution of after
lifecycle callbacks of the underlying test framework — for example,
tearing down test fixtures, ending a transaction, etc.
|
void |
beforeTestClass()
Hook for pre-processing a test class before execution of any
tests within the class.
|
void |
beforeTestExecution(java.lang.Object testInstance,
java.lang.reflect.Method testMethod)
Hook for pre-processing a test immediately before execution of
the test method in the supplied
test context — for example, for timing
or logging purposes.
|
void |
beforeTestMethod(java.lang.Object testInstance,
java.lang.reflect.Method testMethod)
Hook for pre-processing a test before execution of before
lifecycle callbacks of the underlying test framework — for example,
setting up test fixtures, starting a transaction, etc.
|
TestContext |
getTestContext()
Get the
TestContext managed by this TestContextManager . |
java.util.List<TestExecutionListener> |
getTestExecutionListeners()
Get the current
TestExecutionListeners
registered for this TestContextManager . |
void |
prepareTestInstance(java.lang.Object testInstance)
Hook for preparing a test instance prior to execution of any individual
test methods, for example for injecting dependencies, etc.
|
void |
registerTestExecutionListeners(java.util.List<TestExecutionListener> testExecutionListeners)
Register the supplied list of
TestExecutionListeners
by appending them to the list of listeners used by this TestContextManager . |
void |
registerTestExecutionListeners(TestExecutionListener... testExecutionListeners)
Register the supplied array of
TestExecutionListeners
by appending them to the list of listeners used by this TestContextManager . |
public TestContextManager(java.lang.Class<?> testClass)
TestContextManager
for the supplied test class.
Delegates to TestContextManager(TestContextBootstrapper)
with
the TestContextBootstrapper
configured for the test class. If the
@BootstrapWith
annotation is present on the test
class, either directly or as a meta-annotation, then its
value
will be used as the bootstrapper type;
otherwise, the DefaultTestContextBootstrapper
will be used.
testClass
- the test class to be managedTestContextManager(TestContextBootstrapper)
public TestContextManager(TestContextBootstrapper testContextBootstrapper)
TestContextManager
using the supplied TestContextBootstrapper
and register the necessary
TestExecutionListeners
.
Delegates to the supplied TestContextBootstrapper
for building
the TestContext
and retrieving the TestExecutionListeners
.
testContextBootstrapper
- the bootstrapper to useTestContextBootstrapper.buildTestContext()
,
TestContextBootstrapper.getTestExecutionListeners()
,
registerTestExecutionListeners(java.util.List<org.springframework.test.context.TestExecutionListener>)
public final TestContext getTestContext()
TestContext
managed by this TestContextManager
.public void registerTestExecutionListeners(java.util.List<TestExecutionListener> testExecutionListeners)
TestExecutionListeners
by appending them to the list of listeners used by this TestContextManager
.public void registerTestExecutionListeners(TestExecutionListener... testExecutionListeners)
TestExecutionListeners
by appending them to the list of listeners used by this TestContextManager
.public final java.util.List<TestExecutionListener> getTestExecutionListeners()
TestExecutionListeners
registered for this TestContextManager
.
Allows for modifications, e.g. adding a listener to the beginning of the list. However, make sure to keep the list stable while actually executing tests.
public void beforeTestClass() throws java.lang.Exception
@BeforeClass
).
An attempt will be made to give each registered
TestExecutionListener
a chance to pre-process the test class
execution. If a listener throws an exception, however, the remaining
registered listeners will not be called.
java.lang.Exception
- if a registered TestExecutionListener throws an
exceptiongetTestExecutionListeners()
public void prepareTestInstance(java.lang.Object testInstance) throws java.lang.Exception
The managed TestContext
will be updated with the supplied
testInstance
.
An attempt will be made to give each registered
TestExecutionListener
a chance to prepare the test instance. If a
listener throws an exception, however, the remaining registered listeners
will not be called.
testInstance
- the test instance to prepare (never null
)java.lang.Exception
- if a registered TestExecutionListener throws an exceptiongetTestExecutionListeners()
public void beforeTestMethod(java.lang.Object testInstance, java.lang.reflect.Method testMethod) throws java.lang.Exception
This method must be called immediately prior to
framework-specific before lifecycle callbacks (e.g., methods
annotated with JUnit 4's @Before
). For historical
reasons, this method is named beforeTestMethod
. Since the
introduction of beforeTestExecution(java.lang.Object, java.lang.reflect.Method)
, a more suitable name for
this method might be something like beforeTestSetUp
or
beforeEach
; however, it is unfortunately impossible to rename
this method due to backward compatibility concerns.
The managed TestContext
will be updated with the supplied
testInstance
and testMethod
.
An attempt will be made to give each registered
TestExecutionListener
a chance to perform its pre-processing.
If a listener throws an exception, however, the remaining registered
listeners will not be called.
testInstance
- the current test instance (never null
)testMethod
- the test method which is about to be executed on the
test instancejava.lang.Exception
- if a registered TestExecutionListener throws an exceptionafterTestMethod(java.lang.Object, java.lang.reflect.Method, java.lang.Throwable)
,
beforeTestExecution(java.lang.Object, java.lang.reflect.Method)
,
afterTestExecution(java.lang.Object, java.lang.reflect.Method, java.lang.Throwable)
,
getTestExecutionListeners()
public void beforeTestExecution(java.lang.Object testInstance, java.lang.reflect.Method testMethod) throws java.lang.Exception
This method must be called after framework-specific
before lifecycle callbacks (e.g., methods annotated with JUnit 4's
@Before
).
The managed TestContext
will be updated with the supplied
testInstance
and testMethod
.
An attempt will be made to give each registered
TestExecutionListener
a chance to perform its pre-processing.
If a listener throws an exception, however, the remaining registered
listeners will not be called.
testInstance
- the current test instance (never null
)testMethod
- the test method which is about to be executed on the
test instancejava.lang.Exception
- if a registered TestExecutionListener throws an exceptionbeforeTestMethod(java.lang.Object, java.lang.reflect.Method)
,
afterTestMethod(java.lang.Object, java.lang.reflect.Method, java.lang.Throwable)
,
beforeTestExecution(java.lang.Object, java.lang.reflect.Method)
,
afterTestExecution(java.lang.Object, java.lang.reflect.Method, java.lang.Throwable)
,
getTestExecutionListeners()
public void afterTestExecution(java.lang.Object testInstance, java.lang.reflect.Method testMethod, @Nullable java.lang.Throwable exception) throws java.lang.Exception
This method must be called before framework-specific
after lifecycle callbacks (e.g., methods annotated with JUnit 4's
@After
).
The managed TestContext
will be updated with the supplied
testInstance
, testMethod
, and exception
.
Each registered TestExecutionListener
will be given a chance
to perform its post-processing. If a listener throws an exception, the
remaining registered listeners will still be called. After all listeners
have executed, the first caught exception will be rethrown with any
subsequent exceptions suppressed in
the first exception.
Note that registered listeners will be executed in the opposite order in which they were registered.
testInstance
- the current test instance (never null
)testMethod
- the test method which has just been executed on the
test instanceexception
- the exception that was thrown during execution of the
test method or by a TestExecutionListener, or null
if none
was thrownjava.lang.Exception
- if a registered TestExecutionListener throws an exceptionbeforeTestMethod(java.lang.Object, java.lang.reflect.Method)
,
afterTestMethod(java.lang.Object, java.lang.reflect.Method, java.lang.Throwable)
,
beforeTestExecution(java.lang.Object, java.lang.reflect.Method)
,
getTestExecutionListeners()
,
Throwable.addSuppressed(Throwable)
public void afterTestMethod(java.lang.Object testInstance, java.lang.reflect.Method testMethod, @Nullable java.lang.Throwable exception) throws java.lang.Exception
This method must be called immediately after
framework-specific after lifecycle callbacks (e.g., methods
annotated with JUnit 4's @After
). For historical
reasons, this method is named afterTestMethod
. Since the
introduction of afterTestExecution(java.lang.Object, java.lang.reflect.Method, java.lang.Throwable)
, a more suitable name for
this method might be something like afterTestTearDown
or
afterEach
; however, it is unfortunately impossible to rename
this method due to backward compatibility concerns.
The managed TestContext
will be updated with the supplied
testInstance
, testMethod
, and exception
.
Each registered TestExecutionListener
will be given a chance
to perform its post-processing. If a listener throws an exception, the
remaining registered listeners will still be called. After all listeners
have executed, the first caught exception will be rethrown with any
subsequent exceptions suppressed in
the first exception.
Note that registered listeners will be executed in the opposite
testInstance
- the current test instance (never null
)testMethod
- the test method which has just been executed on the
test instanceexception
- the exception that was thrown during execution of the test
method or by a TestExecutionListener, or null
if none was thrownjava.lang.Exception
- if a registered TestExecutionListener throws an exceptionbeforeTestMethod(java.lang.Object, java.lang.reflect.Method)
,
beforeTestExecution(java.lang.Object, java.lang.reflect.Method)
,
afterTestExecution(java.lang.Object, java.lang.reflect.Method, java.lang.Throwable)
,
getTestExecutionListeners()
,
Throwable.addSuppressed(Throwable)
public void afterTestClass() throws java.lang.Exception
@AfterClass
).
Each registered TestExecutionListener
will be given a chance
to perform its post-processing. If a listener throws an exception, the
remaining registered listeners will still be called. After all listeners
have executed, the first caught exception will be rethrown with any
subsequent exceptions suppressed in
the first exception.
Note that registered listeners will be executed in the opposite
java.lang.Exception
- if a registered TestExecutionListener throws an exceptiongetTestExecutionListeners()
,
Throwable.addSuppressed(Throwable)