Package org.springframework.test.context

# Interface TestContext

All Superinterfaces:
  `org.springframework.core.AttributeAccessor`, `Serializable`

All Known Implementing Classes:
  `DefaultTestContext`

public interface TestContext extends org.springframework.core.AttributeAccessor, Serializable
`TestContext` encapsulates the context in which a test is executed, agnostic of the actual testing framework in use.

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.

As of Spring Framework 6.1, concrete implementations are highly encouraged to override `setMethodInvoker(MethodInvoker)` and `getMethodInvoker()`.

Since:
  2.5
Author:
  Sam Brannen
See Also:
  - `TestContextManager`
  - `TestExecutionListener`

## Method Summary

Modifier and Type
Method
Description
`org.springframework.context.ApplicationContext`
`getApplicationContext()`
Get the application context for this test context, possibly cached.
`default MethodInvoker`
`getMethodInvoker()`
Get the `MethodInvoker` to use.
`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 org.springframework.context.ApplicationEvent> eventFactory)`
Publish the `ApplicationEvent` created by the given `eventFactory` to the application context for this test context.
`default void`
`setMethodInvoker(MethodInvoker methodInvoker)`
Set the `MethodInvoker` to use.
`void`
`updateState(Object testInstance, Method testMethod, Throwable testException)`
Update this test context to reflect the state of the currently executing test.

### Methods inherited from interface org.springframework.core.AttributeAccessor

`attributeNames, computeAttribute, getAttribute, hasAttribute, removeAttribute, setAttribute`

## Method Details

### hasApplicationContext

default boolean hasApplicationContext()
Determine if the application context for this test context is known to be available.

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.

Returns:
  `true` if the application context has already been loaded
Since:
  5.2
See Also:
  - `getApplicationContext()`

### getApplicationContext

org.springframework.context.ApplicationContext getApplicationContext()
Get the application context for this test context, possibly cached.

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.

Returns:
  the application context (never `null`)
Throws:
  `IllegalStateException` - if an error occurs while retrieving the application context
See Also:
  - `hasApplicationContext()`

### publishEvent

default void publishEvent(Function<TestContext,? extends org.springframework.context.ApplicationEvent> eventFactory)
Publish the `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.

Parameters:
  `eventFactory` - factory for lazy creation of the `ApplicationEvent`
Since:
  5.2
See Also:
  - `hasApplicationContext()`
  - `getApplicationContext()`

### getTestClass

Class<?> getTestClass()
Get the test class for this test context.

Since JUnit Jupiter 5.12, if the `SpringExtension` is used with a test-method scoped `ExtensionContext`, the `Class` returned from this method may refer to the test class for the current test method, which may be a `@Nested` test class within the class for the test instance. Thus, if you need consistent access to the class for the current test instance within an implementation of `TestExecutionListener.prepareTestInstance(TestContext)`, you should invoke `testContext.getTestInstance().getClass()` instead of `testContext.getTestClass()`.

Returns:
  the test class (never `null`)

### getTestInstance

Object getTestInstance()
Get the current test instance for this test context.

Note: this is a mutable property.

Returns:
  the current test instance (never `null`)
See Also:
  - `updateState(Object, Method, Throwable)`

### getTestMethod

Method getTestMethod()
Get the current test method for this test context.

Note: this is a mutable property.

Returns:
  the current test method (never `null`)
See Also:
  - `updateState(Object, Method, Throwable)`

### getTestException

@Nullable Throwable getTestException()
Get the exception that was thrown during execution of the test method.

Note: this is a mutable property.

Returns:
  the exception that was thrown, or `null` if no exception was thrown
See Also:
  - `updateState(Object, Method, Throwable)`

### markApplicationContextDirty

void markApplicationContextDirty(@Nullable 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.

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.

Parameters:
  `hierarchyMode` - the context cache clearing mode to be applied if the context is part of a hierarchy (may be `null`)

### updateState

void updateState(@Nullable Object testInstance, @Nullable Method testMethod, @Nullable Throwable testException)
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.

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 `null` if no exception was thrown

### setMethodInvoker

default void setMethodInvoker(MethodInvoker methodInvoker)
Set the `MethodInvoker` to use.

By default, this method does nothing.

Concrete implementations should track the supplied `MethodInvoker` and return it from `getMethodInvoker()`. Note that the standard `TestContext` implementation in Spring overrides this method appropriately.

Since:
  6.1

### getMethodInvoker

default MethodInvoker getMethodInvoker()
Get the `MethodInvoker` to use.

By default, this method returns `MethodInvoker.DEFAULT_INVOKER`.

Concrete implementations should return the `MethodInvoker` supplied to `setMethodInvoker(MethodInvoker)`. Note that the standard `TestContext` implementation in Spring overrides this method appropriately.

Since:
  6.1