Interface TestInstancePreDestroyCallback
- All Superinterfaces:
Extension
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
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:
-
Method Summary
Modifier and TypeMethodDescriptionvoid
preDestroyTestInstance
(ExtensionContext context) Callback for processing test instances before they are destroyed.static void
preDestroyTestInstances
(ExtensionContext context, Consumer<Object> callback) Utility method for processing all test instances of anExtensionContext
that are not present in any of its parent contexts.
-
Method Details
-
preDestroyTestInstance
Callback for processing test instances before they are destroyed.Contrary to
TestInstancePostProcessor.postProcessTestInstance(java.lang.Object, org.junit.jupiter.api.extension.ExtensionContext)
this method is only called once for eachExtensionContext
even if there are multiple test instances about to be destroyed in case of@Nested
tests. Please use the providedpreDestroyTestInstances(ExtensionContext, Consumer)
utility method to ensure that all test instances are handled.- Parameters:
context
- the current extension context; nevernull
- Throws:
Exception
- See Also:
-
preDestroyTestInstances
@API(status=STABLE, since="5.10") static void preDestroyTestInstances(ExtensionContext context, Consumer<Object> callback) Utility method for processing all test instances of anExtensionContext
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
- the current extension context; nevernull
callback
- the callback to be invoked for every test instance of the current extension context that is about to be destroyed; nevernull
- Since:
- 5.7.1
-