Class DefaultTestContext
- All Implemented Interfaces:
- Serializable,- AttributeAccessor,- TestContext
TestContext interface.- Since:
- 4.0
- Author:
- Sam Brannen, Juergen Hoeller, Rob Harrop
- See Also:
- 
Constructor SummaryConstructorsConstructorDescriptionDefaultTestContext(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 SummaryModifier and TypeMethodDescriptionString[]Return the names of all attributes.<T> TcomputeAttribute(String name, Function<String, T> computeFunction) Compute a new value for the attribute identified bynameif 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 MethodInvokerGet theMethodInvokerto use.final Class<?>Get the test class for this test context.Get 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) Returntrueif the attribute identified bynameexists.voidmarkApplicationContextDirty(@Nullable 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 bynameand return its value.voidsetAttribute(String name, @Nullable Object value) Set the attribute defined bynameto the suppliedvalue.final voidsetMethodInvoker(MethodInvoker methodInvoker) Set theMethodInvokerto use.toString()Provide a String representation of this test context's state.voidupdateState(@Nullable Object testInstance, @Nullable Method testMethod, @Nullable Throwable testException) Update this test context to reflect the state of the currently executing test.Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.springframework.test.context.TestContextpublishEvent
- 
Constructor Details- 
DefaultTestContextCopy 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 supplied- DefaultTestContextis- null
 
- 
DefaultTestContextpublic DefaultTestContext(Class<?> testClass, MergedContextConfiguration mergedConfig, CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate) Construct a newDefaultTestContextfrom the supplied arguments.- Parameters:
- testClass- the test class for this test context
- mergedConfig- the merged application context configuration for this test context
- cacheAwareContextLoaderDelegate- the delegate to use for loading and closing the application context for this test context
 
 
- 
- 
Method Details- 
hasApplicationContextpublic boolean hasApplicationContext()Determine if the application context for this test context is present in the context cache.- Specified by:
- hasApplicationContextin interface- TestContext
- Returns:
- trueif the application context has already been loaded and stored in the context cache
- Since:
- 5.2
- See Also:
 
- 
getApplicationContextGet the application context for this test context.The default implementation delegates to the CacheAwareContextLoaderDelegatethat was supplied when thisTestContextwas constructed.- Specified by:
- getApplicationContextin interface- TestContext
- 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:
 
- 
markApplicationContextDirtyMark 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 interface- TestContext
- Parameters:
- hierarchyMode- the context cache clearing mode to be applied if the context is part of a hierarchy (may be- null)
- See Also:
 
- 
getTestClassDescription copied from interface:TestContextGet the test class for this test context.- Specified by:
- getTestClassin interface- TestContext
- Returns:
- the test class (never null)
 
- 
getTestInstanceDescription copied from interface:TestContextGet the current test instance for this test context.Note: this is a mutable property. - Specified by:
- getTestInstancein interface- TestContext
- Returns:
- the current test instance (never null)
- See Also:
 
- 
getTestMethodDescription copied from interface:TestContextGet the current test method for this test context.Note: this is a mutable property. - Specified by:
- getTestMethodin interface- TestContext
- Returns:
- the current test method (never null)
- See Also:
 
- 
getTestExceptionDescription copied from interface:TestContextGet the exception that was thrown during execution of the test method.Note: this is a mutable property. - Specified by:
- getTestExceptionin interface- TestContext
- Returns:
- the exception that was thrown, or nullif no exception was thrown
- See Also:
 
- 
updateStatepublic 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 interface- TestContext
- Parameters:
- 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- nullif no exception was thrown
 
- 
setMethodInvokerDescription 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 interface- TestContext
 
- 
getMethodInvokerDescription 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 interface- TestContext
 
- 
setAttributeDescription copied from interface:AttributeAccessorSet the attribute defined bynameto the suppliedvalue.If valueisnull, 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:
- setAttributein interface- AttributeAccessor
- Parameters:
- name- the unique attribute key
- value- the attribute value to be attached
 
- 
getAttributeDescription copied from interface:AttributeAccessorGet the value of the attribute identified byname.Return nullif the attribute doesn't exist.- Specified by:
- getAttributein interface- AttributeAccessor
- Parameters:
- name- the unique attribute key
- Returns:
- the current value of the attribute, if any
 
- 
computeAttributeDescription copied from interface:AttributeAccessorCompute a new value for the attribute identified bynameif necessary and set the new value in thisAttributeAccessor.If a value for the attribute identified by namealready 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 be overridden by concrete implementations of this interface. - Specified by:
- computeAttributein interface- AttributeAccessor
- Type Parameters:
- T- the type of the attribute value
- Parameters:
- name- the unique attribute key
- computeFunction- a function that computes a new value for the attribute name; the function must not return a- nullvalue
- Returns:
- the existing value or newly computed value for the named attribute
- See Also:
 
- 
removeAttributeDescription copied from interface:AttributeAccessorRemove the attribute identified bynameand return its value.Return nullif no attribute undernameis found.- Specified by:
- removeAttributein interface- AttributeAccessor
- Parameters:
- name- the unique attribute key
- Returns:
- the last value of the attribute, if any
 
- 
hasAttributeDescription copied from interface:AttributeAccessorReturntrueif the attribute identified bynameexists.Otherwise return false.- Specified by:
- hasAttributein interface- AttributeAccessor
- Parameters:
- name- the unique attribute key
 
- 
attributeNamesDescription copied from interface:AttributeAccessorReturn the names of all attributes.- Specified by:
- attributeNamesin interface- AttributeAccessor
 
- 
toStringProvide a String representation of this test context's state.
 
-