Class DefaultTestContext
- All Implemented Interfaces:
Serializable
,AttributeAccessor
,TestContext
TestContext
interface.- Since:
- 4.0
- Author:
- Sam Brannen, Juergen Hoeller, Rob Harrop
- See Also:
-
Constructor Summary
ConstructorDescriptionDefaultTestContext
(Class<?> testClass, MergedContextConfiguration mergedContextConfiguration, CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate) Construct a newDefaultTestContext
from the supplied arguments.DefaultTestContext
(DefaultTestContext testContext) Copy constructor for creating a newDefaultTestContext
based on the attributes and immutable state of the supplied context. -
Method Summary
Modifier and TypeMethodDescriptionString[]
Return the names of all attributes.<T> T
computeAttribute
(String name, Function<String, T> computeFunction) Compute a new value for the attribute identified byname
if necessary and set the new value in thisAttributeAccessor
.Get the application context for this test context.getAttribute
(String name) Get the value of the attribute identified byname
.final Class<?>
Get the test class for this test context.final Throwable
Get the exception that was thrown during execution of the test method.final Object
Get the current test instance for this test context.final Method
Get the current test method for this test context.boolean
Determine if the application context for this test context is present in the context cache.boolean
hasAttribute
(String name) Returntrue
if the attribute identified byname
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).removeAttribute
(String name) Remove the attribute identified byname
and return its value.void
setAttribute
(String name, Object value) Set the attribute defined byname
to the suppliedvalue
.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.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.springframework.test.context.TestContext
publishEvent
-
Constructor Details
-
DefaultTestContext
Copy constructor for creating a newDefaultTestContext
based on the attributes and immutable state of the supplied context.Immutable state includes all arguments supplied to the standard constructor.
- Throws:
NullPointerException
- if the suppliedDefaultTestContext
isnull
-
DefaultTestContext
public DefaultTestContext(Class<?> testClass, MergedContextConfiguration mergedContextConfiguration, CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate) Construct a newDefaultTestContext
from the supplied arguments.- Parameters:
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 context
-
-
Method Details
-
hasApplicationContext
public boolean hasApplicationContext()Determine if the application context for this test context is present in the context cache.- Specified by:
hasApplicationContext
in interfaceTestContext
- Returns:
true
if the application context has already been loaded and stored in the context cache- Since:
- 5.2
- See Also:
-
getApplicationContext
Get the application context for this test context.The default implementation delegates to the
CacheAwareContextLoaderDelegate
that was supplied when thisTestContext
was constructed.- Specified by:
getApplicationContext
in interfaceTestContext
- Returns:
- the application context (never
null
) - Throws:
IllegalStateException
- if the context returned by the context loader delegate is not active (i.e., has been closed)- See Also:
-
markApplicationContextDirty
Mark the application context associated with this test context as dirty (i.e., by removing it from the context cache and closing it).The default implementation delegates to the
CacheAwareContextLoaderDelegate
that was supplied when thisTestContext
was constructed.- Specified by:
markApplicationContextDirty
in interfaceTestContext
- Parameters:
hierarchyMode
- the context cache clearing mode to be applied if the context is part of a hierarchy (may benull
)- See Also:
-
getTestClass
Description copied from interface:TestContext
Get the test class for this test context.- Specified by:
getTestClass
in interfaceTestContext
- Returns:
- the test class (never
null
)
-
getTestInstance
Description copied from interface:TestContext
Get the current test instance for this test context.Note: this is a mutable property.
- Specified by:
getTestInstance
in interfaceTestContext
- Returns:
- the current test instance (never
null
) - See Also:
-
getTestMethod
Description copied from interface:TestContext
Get the current test method for this test context.Note: this is a mutable property.
- Specified by:
getTestMethod
in interfaceTestContext
- Returns:
- the current test method (never
null
) - See Also:
-
getTestException
Description copied from interface:TestContext
Get the exception that was thrown during execution of the test method.Note: this is a mutable property.
- Specified by:
getTestException
in interfaceTestContext
- Returns:
- the exception that was thrown, or
null
if no exception was thrown - See Also:
-
updateState
public void updateState(@Nullable Object testInstance, @Nullable Method testMethod, @Nullable Throwable testException) Description copied from interface:TestContext
Update this test context to reflect the state of the currently executing test.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.
- Specified by:
updateState
in interfaceTestContext
- Parameters:
testInstance
- the current test instance (may benull
)testMethod
- the current test method (may benull
)testException
- the exception that was thrown in the test method, ornull
if no exception was thrown
-
setAttribute
Description copied from interface:AttributeAccessor
Set the attribute defined byname
to the suppliedvalue
.If
value
isnull
, the attribute isremoved
.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.
- Specified by:
setAttribute
in interfaceAttributeAccessor
- Parameters:
name
- the unique attribute keyvalue
- the attribute value to be attached
-
getAttribute
Description copied from interface:AttributeAccessor
Get the value of the attribute identified byname
.Return
null
if the attribute doesn't exist.- Specified by:
getAttribute
in interfaceAttributeAccessor
- Parameters:
name
- the unique attribute key- Returns:
- the current value of the attribute, if any
-
computeAttribute
Description copied from interface:AttributeAccessor
Compute a new value for the attribute identified byname
if necessary and set the new value in thisAttributeAccessor
.If a value for the attribute identified by
name
already exists in thisAttributeAccessor
, 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.
- Specified by:
computeAttribute
in interfaceAttributeAccessor
- Type Parameters:
T
- the type of the attribute value- Parameters:
name
- the unique attribute keycomputeFunction
- a function that computes a new value for the attribute name; the function must not return anull
value- Returns:
- the existing value or newly computed value for the named attribute
- See Also:
-
removeAttribute
Description copied from interface:AttributeAccessor
Remove the attribute identified byname
and return its value.Return
null
if no attribute undername
is found.- Specified by:
removeAttribute
in interfaceAttributeAccessor
- Parameters:
name
- the unique attribute key- Returns:
- the last value of the attribute, if any
-
hasAttribute
Description copied from interface:AttributeAccessor
Returntrue
if the attribute identified byname
exists.Otherwise return
false
.- Specified by:
hasAttribute
in interfaceAttributeAccessor
- Parameters:
name
- the unique attribute key
-
attributeNames
Description copied from interface:AttributeAccessor
Return the names of all attributes.- Specified by:
attributeNames
in interfaceAttributeAccessor
-
toString
Provide a String representation of this test context's state.
-