Interface TestInstantiationAwareExtension
- All Superinterfaces:
Extension
- All Known Subinterfaces:
InvocationInterceptor,ParameterResolver,TestInstanceFactory,TestInstancePostProcessor,TestInstancePreConstructCallback
- All Known Implementing Classes:
TypeBasedParameterResolver
TestInstantiationAwareExtension defines the API for Extensions that are aware of or influence the instantiation of test classes.
This interface is not intended to be implemented directly. Instead, extensions should implement one of the sub-interfaces listed below.
InvocationInterceptorParameterResolverTestInstancePreConstructCallbackTestInstancePostProcessorTestInstanceFactory
See getTestInstantiationExtensionContextScope(ExtensionContext) for
further details.
- Since:
- 5.12
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic enumExtensionContextScopeis used to define the scope of theExtensionContextsupplied to an extension during the instantiation of test classes or processing of test instances. -
Method Summary
Modifier and TypeMethodDescriptionDetermine whether this extension should receive a test-method scopedExtensionContextduring the instantiation of test classes or processing of test instances.
-
Method Details
-
getTestInstantiationExtensionContextScope
@API(status=MAINTAINED, since="5.13.3") default TestInstantiationAwareExtension.ExtensionContextScope getTestInstantiationExtensionContextScope(ExtensionContext rootContext) Determine whether this extension should receive a test-method scopedExtensionContextduring the instantiation of test classes or processing of test instances.If an extension returns
TEST_METHODfrom this method, methods defined in the following extension APIs will be called with a test-method scopedExtensionContextinstead of a test-class scoped context. Note, however, that a test-class scoped context will always be supplied if thePER_CLASStest instance lifecycle is used.InvocationInterceptor: only theinterceptTestClassConstructor(...)methodParameterResolver: only when resolving constructor parametersTestInstancePreConstructCallbackTestInstancePostProcessorTestInstanceFactory
When a test-method scoped
ExtensionContextis supplied, implementations of the above extension APIs will observe the following differences.-
getElement()may refer to the test method. -
getTestClass()may refer to a nested test class.-
For
TestInstancePostProcessor, usetestInstance.getClass()to get the test class associated with the supplied instance. -
For
TestInstanceFactoryandTestInstancePreConstructCallback, useTestInstanceFactoryContext.getTestClass()to get the class under construction. -
For
ParameterResolver, when resolving a parameter for a constructor, ensure that theExecutableis aConstructor, and then useconstructor.getDeclaringClass()to get the test class associated with the constructor.
-
For
-
getTestMethod()is no longer empty, unless thePER_CLASStest instance lifecycle is used. -
If the extension adds a
CloseableResourceorAutoCloseableto theStore(unless thejunit.jupiter.extensions.store.close.autocloseable.enabledconfiguration parameter is set tofalse), then the resource will be closed just after the instance is destroyed. -
Extensions can now access data previously stored by a
TestTemplateInvocationContext, unless thePER_CLASStest instance lifecycle 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 authors are therefore advised to opt into this feature, even if they do not 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 will be called.
Therefore, implementations should be idempotent and avoid side effects.
If computation of the return value is costly, implementations may wish to
cache the result in the
Storeof the suppliedExtensionContext.
-