Interface TestInstantiationAwareExtension
- All Superinterfaces:
Extension
- All Known Subinterfaces:
InvocationInterceptor, ParameterResolver, TestInstanceFactory, TestInstancePostProcessor, TestInstancePreConstructCallback
- All Known Implementing Classes:
TypeBasedParameterResolver
@API(status=MAINTAINED,
since="5.13.3")
public interface TestInstantiationAwareExtension
extends Extension
Interface for
Extensions that are aware and can influence
the instantiation of test instances.
This interface is not indented to be implemented directly. Instead, extend one of its sub-interfaces.
- Since:
- 5.12
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic enumExtensionContextScopeis used to define the scope of theExtensionContextpassed to an extension during the instantiation of test instances. -
Method Summary
Modifier and TypeMethodDescriptionWhether this extension should receive a test-scopedExtensionContextduring the instantiation of test instances.
-
Method Details
-
getTestInstantiationExtensionContextScope
@API(status=MAINTAINED, since="5.13.3") default TestInstantiationAwareExtension.ExtensionContextScope getTestInstantiationExtensionContextScope(ExtensionContext rootContext) Whether this extension should receive a test-scopedExtensionContextduring the instantiation of test instances.If an extension returns
TEST_METHODfrom this method, the following extension methods will be called with a test-scopedExtensionContextinstead of a class-scoped one, unless thePER_CLASSlifecycle is used:InvocationInterceptor.interceptTestClassConstructor(InvocationInterceptor.Invocation, ReflectiveInvocationContext, ExtensionContext)ParameterResolverwhen resolving constructor parametersTestInstancePreConstructCallbackTestInstancePostProcessorTestInstanceFactory
In such cases, implementations of these extension callbacks can observe the following differences:
getElement()may refer to the test method andgetTestClass()may refer to a nested test class. UseTestInstanceFactoryContext.getTestClass()to get the class under construction.getTestMethod()is no longer empty, unless thePER_CLASSlifecycle is used.- If the callback adds a new
ExtensionContext.Store.CloseableResourceorAutoCloseableto theStore(unless thejunit.jupiter.extensions.store.close.autocloseable.enabledconfiguration parameter is set tofalse), then the resource is closed just after the instance is destroyed. - The callbacks can now access data previously stored by
TestTemplateInvocationContext, unless thePER_CLASSlifecycle is used.
Note: The behavior which is enabled by returning
TEST_METHODfrom this method will become the default in future versions of JUnit. To ensure forward compatibility, extension implementors are therefore advised to opt in, even if they don't require the new functionality.- Parameters:
rootContext- the root extension context to allow inspection of configuration parameters; nevernull- Since:
- 5.12
- Implementation Note:
- There are no guarantees about how often this method is called.
Therefore, implementations should be idempotent and avoid side
effects. They may, however, cache the result for performance in
the
Storeof the suppliedExtensionContext, if necessary.
-