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.
@FunctionalInterface @API(status=STABLE, since="5.7") public interface TestInstancePreDestroyCallback extends ExtensionTestInstancePreDestroyCallbackdefines the API forExtensionsthat 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
TestInstancePreDestroyCallbackmust 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,TestInstancePreDestroyCallbackextensions may be registered at the class level or at the method level. In the latter case, theTestInstancePreDestroyCallbackextension will only be applied to the test method for which it is registered.Constructor Requirements
Consult the documentation in
Extensionfor details on constructor requirements.- Since:
- 5.6
- See Also:
preDestroyTestInstance(ExtensionContext),TestInstancePostProcessor,TestInstanceFactory,ParameterResolver
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description voidpreDestroyTestInstance(ExtensionContext context)Callback for processing test instances before they are destroyed.static voidpreDestroyTestInstances(ExtensionContext context, java.util.function.Consumer<java.lang.Object> callback)Utility method for processing all test instances of anExtensionContextthat are not present in any of its parent contexts.
-
-
-
Method Detail
-
preDestroyTestInstance
void preDestroyTestInstance(ExtensionContext context) throws java.lang.Exception
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 eachExtensionContexteven if there are multiple test instances about to be destroyed in case of@Nestedtests. Please use the providedpreDestroyTestInstances(ExtensionContext, Consumer)utility method to ensure that all test instances are handled.- Parameters:
context- the current extension context; nevernull- Throws:
java.lang.Exception- See Also:
ExtensionContext.getTestInstance(),ExtensionContext.getRequiredTestInstance(),ExtensionContext.getTestInstances(),ExtensionContext.getRequiredTestInstances(),preDestroyTestInstances(ExtensionContext, Consumer)
-
preDestroyTestInstances
@API(status=EXPERIMENTAL, since="5.7.1") static void preDestroyTestInstances(ExtensionContext context, java.util.function.Consumer<java.lang.Object> callback)Utility method for processing all test instances of anExtensionContextthat 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; nevernullcallback- 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
-
-