@FunctionalInterface
@API(status=STABLE,
since="5.7")
public interface TestInstancePreDestroyCallback
extends Extension
TestInstancePreDestroyCallback defines the API for Extensions that wish to process test instances after they have been
used in tests but before they are destroyed.
Common use cases include releasing resources that have been created for the test instance, invoking custom clean-up methods on the test instance, etc.
Extensions that implement TestInstancePreDestroyCallback must be
registered at the class level if the test class is configured with
@TestInstance(Lifecycle.PER_CLASS)
semantics. If the test class is configured with
@TestInstance(Lifecycle.PER_METHOD)
semantics, TestInstancePreDestroyCallback extensions may be registered
at the class level or at the method level. In the latter case, the
TestInstancePreDestroyCallback extension will only be applied to the
test method for which it is registered.
A symmetric TestInstancePreConstructCallback extension defines a callback
hook that is invoked prior to any test class instances being constructed.
Consult the documentation in Extension for details on constructor
requirements.
| Modifier and Type | Method and Description |
|---|---|
void |
preDestroyTestInstance(ExtensionContext context)
Callback for processing test instances before they are destroyed.
|
static void |
preDestroyTestInstances(ExtensionContext context,
java.util.function.Consumer<java.lang.Object> callback)
Utility method for processing all test instances of an
ExtensionContext that are not present in any of its parent
contexts. |
void preDestroyTestInstance(ExtensionContext context) throws java.lang.Exception
Contrary to TestInstancePostProcessor.postProcessTestInstance(java.lang.Object, org.junit.jupiter.api.extension.ExtensionContext)
this method is only called once for each ExtensionContext even if
there are multiple test instances about to be destroyed in case of
@Nested tests. Please use the provided
preDestroyTestInstances(ExtensionContext, Consumer) utility
method to ensure that all test instances are handled.
context - the current extension context; never nulljava.lang.ExceptionExtensionContext.getTestInstance(),
ExtensionContext.getRequiredTestInstance(),
ExtensionContext.getTestInstances(),
ExtensionContext.getRequiredTestInstances(),
preDestroyTestInstances(ExtensionContext, Consumer)@API(status=STABLE,
since="5.10")
static void preDestroyTestInstances(ExtensionContext context,
java.util.function.Consumer<java.lang.Object> callback)
ExtensionContext that are not present in any of its parent
contexts.
This method should be called in order to implement this interface correctly since it ensures that the right test instances are processed regardless of the used lifecycle. The supplied callback is called once per test instance that is about to be destroyed starting with the innermost one.
This method is intended to be called from an implementation of
preDestroyTestInstance(ExtensionContext) like this:
{@code class MyExtension implements TestInstancePreDestroyCallback {context - the current extension context; never nullcallback - the callback to be invoked for every test instance of the
current extension context that is about to be destroyed; never
null