public interface TestContext extends AttributeAccessor, Serializable
TestContext
encapsulates the context in which a test is executed,
agnostic of the actual testing framework in use.
As of Spring Framework 5.0, concrete implementations are highly encouraged
to implement a copy constructor in order to allow the immutable state
and attributes of a TestContext
to be used as a template for additional
contexts created for parallel test execution. The copy constructor must accept a
single argument of the type of the concrete implementation. Any implementation
that does not provide a copy constructor will likely fail in an environment
that executes tests concurrently.
TestContextManager
,
TestExecutionListener
Modifier and Type | Method and Description |
---|---|
ApplicationContext |
getApplicationContext()
Get the application context for this
test context, possibly cached.
|
Class<?> |
getTestClass()
Get the test class for this test context.
|
Throwable |
getTestException()
Get the exception that was thrown during execution
of the test method.
|
Object |
getTestInstance()
Get the current test instance for this test context.
|
Method |
getTestMethod()
Get the current test method for this test context.
|
default boolean |
hasApplicationContext()
Determine if the application context for
this test context is known to be available.
|
void |
markApplicationContextDirty(DirtiesContext.HierarchyMode hierarchyMode)
Call this method to signal that the application
context associated with this test context is dirty and should be
removed from the context cache.
|
default void |
publishEvent(Function<TestContext,? extends ApplicationEvent> eventFactory)
Publish the
ApplicationEvent created by the given eventFactory
to the application context for this
test context. |
void |
updateState(Object testInstance,
Method testMethod,
Throwable testException)
Update this test context to reflect the state of the currently executing test.
|
attributeNames, computeAttribute, getAttribute, hasAttribute, removeAttribute, setAttribute
default boolean hasApplicationContext()
If this method returns true
, a subsequent invocation of
getApplicationContext()
should succeed.
The default implementation of this method always returns false
.
Custom TestContext
implementations are therefore highly encouraged
to override this method with a more meaningful implementation. Note that
the standard TestContext
implementation in Spring overrides this
method appropriately.
true
if the application context has already been loadedgetApplicationContext()
ApplicationContext getApplicationContext()
Implementations of this method are responsible for loading the application context if the corresponding context has not already been loaded, potentially caching the context as well.
null
)IllegalStateException
- if an error occurs while retrieving the
application contexthasApplicationContext()
default void publishEvent(Function<TestContext,? extends ApplicationEvent> eventFactory)
ApplicationEvent
created by the given eventFactory
to the application context for this
test context.
The ApplicationEvent
will only be published if the application
context for this test context is available.
eventFactory
- factory for lazy creation of the ApplicationEvent
hasApplicationContext()
,
getApplicationContext()
Class<?> getTestClass()
null
)Object getTestInstance()
Note: this is a mutable property.
null
)updateState(Object, Method, Throwable)
Method getTestMethod()
Note: this is a mutable property.
null
)updateState(Object, Method, Throwable)
@Nullable Throwable getTestException()
Note: this is a mutable property.
null
if no exception was thrownupdateState(Object, Method, Throwable)
void markApplicationContextDirty(@Nullable DirtiesContext.HierarchyMode hierarchyMode)
Do this if a test has modified the context — for example, by modifying the state of a singleton bean, modifying the state of an embedded database, etc.
hierarchyMode
- the context cache clearing mode to be applied if the
context is part of a hierarchy (may be null
)void updateState(@Nullable Object testInstance, @Nullable Method testMethod, @Nullable Throwable testException)
WARNING: This method should only be invoked by the
TestContextManager
.
Caution: concurrent invocations of this method might not be thread-safe, depending on the underlying implementation.
testInstance
- the current test instance (may be null
)testMethod
- the current test method (may be null
)testException
- the exception that was thrown in the test method,
or null
if no exception was thrown