Module org.junit.jupiter.api
Package org.junit.jupiter.api.extension
Interface TestInstantiationAwareExtension
- All Superinterfaces:
Extension
- All Known Subinterfaces:
InvocationInterceptor
,ParameterResolver
,TestInstanceFactory
,TestInstancePostProcessor
,TestInstancePreConstructCallback
- All Known Implementing Classes:
TypeBasedParameterResolver
@API(status=EXPERIMENTAL,
since="5.12")
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 enum
ExtensionContextScope
is used to define the scope of theExtensionContext
passed to an extension during the instantiation of test instances. -
Method Summary
Modifier and TypeMethodDescriptionWhether this extension should receive a test-scopedExtensionContext
during the instantiation of test instances.
-
Method Details
-
getTestInstantiationExtensionContextScope
@API(status=EXPERIMENTAL, since="5.12") default TestInstantiationAwareExtension.ExtensionContextScope getTestInstantiationExtensionContextScope(ExtensionContext rootContext) Whether this extension should receive a test-scopedExtensionContext
during the instantiation of test instances.If an extension returns
TEST_METHOD
from this method, the following extension methods will be called with a test-scopedExtensionContext
instead of a class-scoped one, unless thePER_CLASS
lifecycle is used:InvocationInterceptor.interceptTestClassConstructor(org.junit.jupiter.api.extension.InvocationInterceptor.Invocation<T>, org.junit.jupiter.api.extension.ReflectiveInvocationContext<java.lang.reflect.Constructor<T>>, org.junit.jupiter.api.extension.ExtensionContext)
ParameterResolver
when resolving constructor parametersTestInstancePreConstructCallback
TestInstancePostProcessor
TestInstanceFactory
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_CLASS
lifecycle is used.- If the callback adds a new
ExtensionContext.Store.CloseableResource
to theStore
, the resource is closed just after the instance is destroyed. - The callbacks can now access data previously stored by
TestTemplateInvocationContext
, unless thePER_CLASS
lifecycle is used.
Note: The behavior which is enabled by returning
TEST_METHOD
from 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
Store
of the suppliedExtensionContext
, if necessary.
-