public class DefaultTestContext extends Object implements TestContext
TestContext
interface.Constructor and Description |
---|
DefaultTestContext(Class<?> testClass,
MergedContextConfiguration mergedContextConfiguration,
CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate)
Construct a new
DefaultTestContext from the supplied arguments. |
DefaultTestContext(DefaultTestContext testContext)
Copy constructor for creating a new
DefaultTestContext
based on the attributes and immutable state of the supplied context. |
Modifier and Type | Method and Description |
---|---|
String[] |
attributeNames()
Return the names of all attributes.
|
<T> T |
computeAttribute(String name,
Function<String,T> computeFunction)
Compute a new value for the attribute identified by
name if
necessary and set the new value in this
AttributeAccessor . |
ApplicationContext |
getApplicationContext()
Get the application context for this
test context.
|
Object |
getAttribute(String name)
Get the value of the attribute identified by
name . |
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.
|
boolean |
hasApplicationContext()
Determine if the application context for
this test context is present in the context cache.
|
boolean |
hasAttribute(String name)
Return
true if the attribute identified by name exists. |
void |
markApplicationContextDirty(DirtiesContext.HierarchyMode hierarchyMode)
Mark the application context associated
with this test context as dirty (i.e., by removing it from the
context cache and closing it).
|
Object |
removeAttribute(String name)
Remove the attribute identified by
name and return its value. |
void |
setAttribute(String name,
Object value)
Set the attribute defined by
name to the supplied value . |
String |
toString()
Provide a String representation of this test context's state.
|
void |
updateState(Object testInstance,
Method testMethod,
Throwable testException)
Update this test context to reflect the state of the currently executing test.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
publishEvent
public DefaultTestContext(DefaultTestContext testContext)
DefaultTestContext
based on the attributes and immutable state of the supplied context.
Immutable state includes all arguments supplied to the standard constructor.
NullPointerException
- if the supplied DefaultTestContext
is null
public DefaultTestContext(Class<?> testClass, MergedContextConfiguration mergedContextConfiguration, CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate)
DefaultTestContext
from the supplied arguments.testClass
- the test class for this test contextmergedContextConfiguration
- the merged application context
configuration for this test contextcacheAwareContextLoaderDelegate
- the delegate to use for loading
and closing the application context for this test contextpublic boolean hasApplicationContext()
hasApplicationContext
in interface TestContext
true
if the application context has already been loaded
and stored in the context cachegetApplicationContext()
,
CacheAwareContextLoaderDelegate.isContextLoaded(org.springframework.test.context.MergedContextConfiguration)
public ApplicationContext getApplicationContext()
The default implementation delegates to the CacheAwareContextLoaderDelegate
that was supplied when this TestContext
was constructed.
getApplicationContext
in interface TestContext
null
)IllegalStateException
- if the context returned by the context
loader delegate is not active (i.e., has been closed)CacheAwareContextLoaderDelegate.loadContext(org.springframework.test.context.MergedContextConfiguration)
public void markApplicationContextDirty(@Nullable DirtiesContext.HierarchyMode hierarchyMode)
The default implementation delegates to the CacheAwareContextLoaderDelegate
that was supplied when this TestContext
was constructed.
markApplicationContextDirty
in interface TestContext
hierarchyMode
- the context cache clearing mode to be applied if the
context is part of a hierarchy (may be null
)CacheAwareContextLoaderDelegate.closeContext(org.springframework.test.context.MergedContextConfiguration, org.springframework.test.annotation.DirtiesContext.HierarchyMode)
public final Class<?> getTestClass()
TestContext
getTestClass
in interface TestContext
null
)public final Object getTestInstance()
TestContext
Note: this is a mutable property.
getTestInstance
in interface TestContext
null
)TestContext.updateState(Object, Method, Throwable)
public final Method getTestMethod()
TestContext
Note: this is a mutable property.
getTestMethod
in interface TestContext
null
)TestContext.updateState(Object, Method, Throwable)
@Nullable public final Throwable getTestException()
TestContext
Note: this is a mutable property.
getTestException
in interface TestContext
null
if no exception was thrownTestContext.updateState(Object, Method, Throwable)
public void updateState(@Nullable Object testInstance, @Nullable Method testMethod, @Nullable Throwable testException)
TestContext
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.
updateState
in interface TestContext
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 thrownpublic void setAttribute(String name, @Nullable Object value)
AttributeAccessor
name
to the supplied value
.
If value
is null
, the attribute is removed
.
In general, users should take care to prevent overlaps with other metadata attributes by using fully-qualified names, perhaps using class or package names as prefix.
setAttribute
in interface AttributeAccessor
name
- the unique attribute keyvalue
- the attribute value to be attached@Nullable public Object getAttribute(String name)
AttributeAccessor
name
.
Return null
if the attribute doesn't exist.
getAttribute
in interface AttributeAccessor
name
- the unique attribute keypublic <T> T computeAttribute(String name, Function<String,T> computeFunction)
AttributeAccessor
name
if
necessary and set the new value in this
AttributeAccessor
.
If a value for the attribute identified by name
already exists
in this AttributeAccessor
, the existing value will be returned
without applying the supplied compute function.
The default implementation of this method is not thread safe but can overridden by concrete implementations of this interface.
computeAttribute
in interface AttributeAccessor
T
- the type of the attribute valuename
- the unique attribute keycomputeFunction
- a function that computes a new value for the attribute
name; the function must not return a null
valueAttributeAccessor.getAttribute(String)
,
AttributeAccessor.setAttribute(String, Object)
@Nullable public Object removeAttribute(String name)
AttributeAccessor
name
and return its value.
Return null
if no attribute under name
is found.
removeAttribute
in interface AttributeAccessor
name
- the unique attribute keypublic boolean hasAttribute(String name)
AttributeAccessor
true
if the attribute identified by name
exists.
Otherwise return false
.
hasAttribute
in interface AttributeAccessor
name
- the unique attribute keypublic String[] attributeNames()
AttributeAccessor
attributeNames
in interface AttributeAccessor