Class DefaultTestContext
- All Implemented Interfaces:
Serializable,org.springframework.core.AttributeAccessor,TestContext
TestContext interface.- Since:
- 4.0
- Author:
- Sam Brannen, Juergen Hoeller, Rob Harrop
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionDefaultTestContext(Class<?> testClass, MergedContextConfiguration mergedConfig, CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate) Construct a newDefaultTestContextfrom the supplied arguments.DefaultTestContext(DefaultTestContext testContext) Copy constructor for creating a newDefaultTestContextbased on the attributes and immutable state of the supplied context. -
Method Summary
Modifier and TypeMethodDescriptionString[]<T> TcomputeAttribute(String name, Function<String, T> computeFunction) org.springframework.context.ApplicationContextGet the application context for this test context.getAttribute(String name) final MethodInvokerGet theMethodInvokerto use.final Class<?>Get the test class for this test context.final ThrowableGet the exception that was thrown during execution of the test method.final ObjectGet the current test instance for this test context.final MethodGet the current test method for this test context.booleanDetermine if the application context for this test context is present in the context cache.booleanhasAttribute(String name) voidmarkApplicationContextDirty(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) voidsetAttribute(String name, Object value) final voidsetMethodInvoker(MethodInvoker methodInvoker) Set theMethodInvokerto use.toString()Provide a String representation of this test context's state.voidupdateState(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, waitMethods inherited from interface org.springframework.test.context.TestContext
publishEvent
-
Constructor Details
-
DefaultTestContext
Copy constructor for creating a newDefaultTestContextbased on the attributes and immutable state of the supplied context.Immutable state includes all arguments supplied to the standard constructor.
- Throws:
NullPointerException- if the suppliedDefaultTestContextisnull
-
DefaultTestContext
public DefaultTestContext(Class<?> testClass, MergedContextConfiguration mergedConfig, CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate) Construct a newDefaultTestContextfrom the supplied arguments.- Parameters:
testClass- the test class for this test contextmergedConfig- 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:
hasApplicationContextin interfaceTestContext- Returns:
trueif the application context has already been loaded and stored in the context cache- Since:
- 5.2
- See Also:
-
getApplicationContext
public org.springframework.context.ApplicationContext getApplicationContext()Get the application context for this test context.The default implementation delegates to the
CacheAwareContextLoaderDelegatethat was supplied when thisTestContextwas constructed.- Specified by:
getApplicationContextin 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
CacheAwareContextLoaderDelegatethat was supplied when thisTestContextwas constructed.- Specified by:
markApplicationContextDirtyin 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:TestContextGet the test class for this test context.- Specified by:
getTestClassin interfaceTestContext- Returns:
- the test class (never
null)
-
getTestInstance
Description copied from interface:TestContextGet the current test instance for this test context.Note: this is a mutable property.
- Specified by:
getTestInstancein interfaceTestContext- Returns:
- the current test instance (never
null) - See Also:
-
getTestMethod
Description copied from interface:TestContextGet the current test method for this test context.Note: this is a mutable property.
- Specified by:
getTestMethodin interfaceTestContext- Returns:
- the current test method (never
null) - See Also:
-
getTestException
Description copied from interface:TestContextGet the exception that was thrown during execution of the test method.Note: this is a mutable property.
- Specified by:
getTestExceptionin interfaceTestContext- Returns:
- the exception that was thrown, or
nullif no exception was thrown - See Also:
-
updateState
public void updateState(@Nullable Object testInstance, @Nullable Method testMethod, @Nullable Throwable testException) Description copied from interface:TestContextUpdate 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:
updateStatein 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, ornullif no exception was thrown
-
setMethodInvoker
Description copied from interface:TestContextSet theMethodInvokerto use.By default, this method does nothing.
Concrete implementations should track the supplied
MethodInvokerand return it fromTestContext.getMethodInvoker(). Note that the standardTestContextimplementation in Spring overrides this method appropriately.- Specified by:
setMethodInvokerin interfaceTestContext
-
getMethodInvoker
Description copied from interface:TestContextGet theMethodInvokerto use.By default, this method returns
MethodInvoker.DEFAULT_INVOKER.Concrete implementations should return the
MethodInvokersupplied toTestContext.setMethodInvoker(MethodInvoker). Note that the standardTestContextimplementation in Spring overrides this method appropriately.- Specified by:
getMethodInvokerin interfaceTestContext
-
setAttribute
- Specified by:
setAttributein interfaceorg.springframework.core.AttributeAccessor
-
getAttribute
- Specified by:
getAttributein interfaceorg.springframework.core.AttributeAccessor
-
computeAttribute
- Specified by:
computeAttributein interfaceorg.springframework.core.AttributeAccessor
-
removeAttribute
- Specified by:
removeAttributein interfaceorg.springframework.core.AttributeAccessor
-
hasAttribute
- Specified by:
hasAttributein interfaceorg.springframework.core.AttributeAccessor
-
attributeNames
- Specified by:
attributeNamesin interfaceorg.springframework.core.AttributeAccessor
-
toString
Provide a String representation of this test context's state.
-