Interface InvocationInterceptor
- All Superinterfaces:
Extension
,TestInstantiationAwareExtension
InvocationInterceptor
defines the API for Extensions
that wish to intercept calls to test code.
Invocation Contract
Each method in this class must call InvocationInterceptor.Invocation.proceed()
or InvocationInterceptor.Invocation.skip()
exactly once on the supplied invocation. Otherwise, the
enclosing test or container will be reported as failed.
The default implementation calls proceed()
on the supplied invocation.
Constructor Requirements
Consult the documentation in Extension
for details on
constructor requirements.
- Since:
- 5.5
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interface
InvocationInterceptor.Invocation<T extends @Nullable Object>
An invocation that returns a result and may throw aThrowable
.Nested classes/interfaces inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension
TestInstantiationAwareExtension.ExtensionContextScope
-
Method Summary
Modifier and TypeMethodDescriptiondefault void
interceptAfterAllMethod
(InvocationInterceptor.Invocation<@Nullable Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) Intercept the invocation of an@AfterAll
method.default void
interceptAfterEachMethod
(InvocationInterceptor.Invocation<@Nullable Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) Intercept the invocation of an@AfterEach
method.default void
interceptBeforeAllMethod
(InvocationInterceptor.Invocation<@Nullable Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) Intercept the invocation of a@BeforeAll
method.default void
interceptBeforeEachMethod
(InvocationInterceptor.Invocation<@Nullable Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) Intercept the invocation of a@BeforeEach
method.default void
interceptDynamicTest
(InvocationInterceptor.Invocation<@Nullable Void> invocation, DynamicTestInvocationContext invocationContext, ExtensionContext extensionContext) Intercept the invocation of aDynamicTest
.default <T> T
interceptTestClassConstructor
(InvocationInterceptor.Invocation<T> invocation, ReflectiveInvocationContext<Constructor<T>> invocationContext, ExtensionContext extensionContext) Intercept the invocation of a test class constructor.default <T extends @Nullable Object>
TinterceptTestFactoryMethod
(InvocationInterceptor.Invocation<T> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) Intercept the invocation of a@TestFactory
method, such as a@RepeatedTest
or@ParameterizedTest
method.default void
interceptTestMethod
(InvocationInterceptor.Invocation<@Nullable Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) Intercept the invocation of a@Test
method.default void
interceptTestTemplateMethod
(InvocationInterceptor.Invocation<@Nullable Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) Intercept the invocation of a@TestTemplate
method.Methods inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension
getTestInstantiationExtensionContextScope
-
Method Details
-
interceptTestClassConstructor
default <T> T interceptTestClassConstructor(InvocationInterceptor.Invocation<T> invocation, ReflectiveInvocationContext<Constructor<T>> invocationContext, ExtensionContext extensionContext) throws Throwable Intercept the invocation of a test class constructor.Note that the test class may not have been initialized (static initialization) when this method is invoked.
By default, the supplied
ExtensionContext
represents the test class that's about to be constructed. Extensions may overrideTestInstantiationAwareExtension.getTestInstantiationExtensionContextScope(org.junit.jupiter.api.extension.ExtensionContext)
to returnTEST_METHOD
in order to change the scope of theExtensionContext
to the test method, unless thePER_CLASS
lifecycle is used. Changing the scope makes test-specific data available to the implementation of this method and allows keeping state on the test level by using the providedStore
instance.- Type Parameters:
T
- the result type- Parameters:
invocation
- the invocation that is being intercepted; nevernull
invocationContext
- the context of the invocation that is being intercepted; nevernull
extensionContext
- the current extension context; nevernull
- Returns:
- the result of the invocation; never
null
- Throws:
Throwable
- in case of failure
-
interceptBeforeAllMethod
default void interceptBeforeAllMethod(InvocationInterceptor.Invocation<@Nullable Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable Intercept the invocation of a@BeforeAll
method.- Parameters:
invocation
- the invocation that is being intercepted; nevernull
invocationContext
- the context of the invocation that is being intercepted; nevernull
extensionContext
- the current extension context; nevernull
- Throws:
Throwable
- in case of failures
-
interceptBeforeEachMethod
default void interceptBeforeEachMethod(InvocationInterceptor.Invocation<@Nullable Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable Intercept the invocation of a@BeforeEach
method.- Parameters:
invocation
- the invocation that is being intercepted; nevernull
invocationContext
- the context of the invocation that is being intercepted; nevernull
extensionContext
- the current extension context; nevernull
- Throws:
Throwable
- in case of failures
-
interceptTestMethod
default void interceptTestMethod(InvocationInterceptor.Invocation<@Nullable Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable Intercept the invocation of a@Test
method.- Parameters:
invocation
- the invocation that is being intercepted; nevernull
invocationContext
- the context of the invocation that is being intercepted; nevernull
extensionContext
- the current extension context; nevernull
- Throws:
Throwable
- in case of failures
-
interceptTestFactoryMethod
default <T extends @Nullable Object> T interceptTestFactoryMethod(InvocationInterceptor.Invocation<T> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable Intercept the invocation of a@TestFactory
method, such as a@RepeatedTest
or@ParameterizedTest
method.- Type Parameters:
T
- the result type- Parameters:
invocation
- the invocation that is being intercepted; nevernull
invocationContext
- the context of the invocation that is being intercepted; nevernull
extensionContext
- the current extension context; nevernull
- Returns:
- the result of the invocation; potentially
null
- Throws:
Throwable
- in case of failures
-
interceptTestTemplateMethod
default void interceptTestTemplateMethod(InvocationInterceptor.Invocation<@Nullable Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable Intercept the invocation of a@TestTemplate
method.- Parameters:
invocation
- the invocation that is being intercepted; nevernull
invocationContext
- the context of the invocation that is being intercepted; nevernull
extensionContext
- the current extension context; nevernull
- Throws:
Throwable
- in case of failures
-
interceptDynamicTest
@API(status=STABLE, since="5.11") default void interceptDynamicTest(InvocationInterceptor.Invocation<@Nullable Void> invocation, DynamicTestInvocationContext invocationContext, ExtensionContext extensionContext) throws Throwable Intercept the invocation of aDynamicTest
.- Parameters:
invocation
- the invocation that is being intercepted; nevernull
invocationContext
- the context of the invocation that is being intercepted; nevernull
extensionContext
- the current extension context; nevernull
- Throws:
Throwable
- in case of failures
-
interceptAfterEachMethod
default void interceptAfterEachMethod(InvocationInterceptor.Invocation<@Nullable Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable Intercept the invocation of an@AfterEach
method.- Parameters:
invocation
- the invocation that is being intercepted; nevernull
invocationContext
- the context of the invocation that is being intercepted; nevernull
extensionContext
- the current extension context; nevernull
- Throws:
Throwable
- in case of failures
-
interceptAfterAllMethod
default void interceptAfterAllMethod(InvocationInterceptor.Invocation<@Nullable Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable Intercept the invocation of an@AfterAll
method.- Parameters:
invocation
- the invocation that is being intercepted; nevernull
invocationContext
- the context of the invocation that is being intercepted; nevernull
extensionContext
- the current extension context; nevernull
- Throws:
Throwable
- in case of failures
-