Top Description Methods
org.junit.jupiter.api.extension

public Interface TestInstancePreDestroyCallback

extends Extension
Annotations
@FunctionalInterface
@API
status:STABLE
since:5.7
Static Imports
org.apiguardian.api.API.Status.STABLE

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.

Constructor Requirements

Consult the documentation in Extension for details on constructor requirements.

Since
5.6
See Also
preDestroyTestInstance(ExtensionContext), TestInstancePostProcessor, TestInstancePreConstructCallback, TestInstanceFactory, ParameterResolver

Method Summary

Modifier and TypeMethod and Description
public void
preDestroyTestInstance(ExtensionContext
the current extension context; never null
context
)

Callback for processing test instances before they are destroyed.

public static void
preDestroyTestInstances(ExtensionContext
the current extension context; never null
context
,
Consumer<Object>
the callback to be invoked for every test instance of the current extension context that is about to be destroyed; never null
callback
)

Utility method for processing all test instances of an ExtensionContext that are not present in any of its parent contexts.

Method Detail

preDestroyTestInstanceback to summary
public void preDestroyTestInstance(ExtensionContext context) throws Exception

Callback for processing test instances before they are destroyed.

Contrary to TestInstancePostProcessor#postProcessTestInstance 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.

Parameters
context:ExtensionContext

the current extension context; never null

See Also
ExtensionContext#getTestInstance(), ExtensionContext#getRequiredTestInstance(), ExtensionContext#getTestInstances(), ExtensionContext#getRequiredTestInstances(), preDestroyTestInstances(ExtensionContext, Consumer)
preDestroyTestInstancesback to summary
public static void preDestroyTestInstances(ExtensionContext context, Consumer<Object> callback)

Utility method for processing all test instances of an 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:

class MyExtension implements TestInstancePreDestroyCallback {
    @Override
    public void preDestroyTestInstance(ExtensionContext context) {
        TestInstancePreDestroyCallback.preDestroyTestInstances(context, testInstance -> {
            // custom logic that processes testInstance
        });
    }
}
Parameters
context:ExtensionContext

the current extension context; never null

callback:Consumer<Object>

the callback to be invoked for every test instance of the current extension context that is about to be destroyed; never null

Annotations
@API
status:STABLE
since:5.10
Since
5.7.1