Debian Codegraph @

junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/InvocationInterceptor.java

raw symbols SCIP
1 /*
2 * Copyright 2015-2024 the original author or authors.
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v2.0 which
6 * accompanies this distribution and is available at
7 *
8 * https://www.eclipse.org/legal/epl-v20.html
9 */
10
11 package org.junit.jupiter.api.extension;
12
13 import static org.apiguardian.api.API.Status.DEPRECATED;
14 import static org.apiguardian.api.API.Status.EXPERIMENTAL;
15 import static org.apiguardian.api.API.Status.STABLE;
16
17 import java.lang.reflect.Constructor;
18 import java.lang.reflect.Method;
19
20 import org.apiguardian.api.API;
21 import org.junit.jupiter.api.AfterAll;
22 import org.junit.jupiter.api.AfterEach;
23 import org.junit.jupiter.api.BeforeAll;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.DynamicTest;
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.api.TestFactory;
28 import org.junit.jupiter.api.TestTemplate;
29
30 /**
31 * {@code InvocationInterceptor} defines the API for {@link Extension
32 * Extensions} that wish to intercept calls to test code.
33 *
34 * <h2>Invocation Contract</h2>
35 *
36 * <p>Each method in this class must call {@link Invocation#proceed()} or {@link
37 * Invocation#skip()} exactly once on the supplied invocation. Otherwise, the
38 * enclosing test or container will be reported as failed.
39 *
40 * <p>The default implementation calls {@link Invocation#proceed()
41 * proceed()} on the supplied {@linkplain Invocation invocation}.
42 *
43 * <h2>Constructor Requirements</h2>
44 *
45 * <p>Consult the documentation in {@link Extension} for details on
46 * constructor requirements.
47 *
48 * @since 5.5
49 * @see Invocation
50 * @see ReflectiveInvocationContext
51 * @see ExtensionContext
52 */
53 @API(status = STABLE, since = "5.10")
54 public interface InvocationInterceptor extends Extension {
55
56 /**
57 * Intercept the invocation of a test class constructor.
58 *
59 * <p>Note that the test class may <em>not</em> have been initialized
60 * (static initialization) when this method is invoked.
61 *
62 * @param invocation the invocation that is being intercepted; never
63 * {@code null}
64 * @param invocationContext the context of the invocation that is being
65 * intercepted; never {@code null}
66 * @param extensionContext the current extension context; never {@code null}
67 * @param <T> the result type
68 * @return the result of the invocation; never {@code null}
69 * @throws Throwable in case of failure
70 */
71 default <T> T interceptTestClassConstructor(Invocation<T> invocation,
72 ReflectiveInvocationContext<Constructor<T>> invocationContext, ExtensionContext extensionContext)
73 throws Throwable {
74 return invocation.proceed();
75 }
76
77 /**
78 * Intercept the invocation of a {@link BeforeAll @BeforeAll} method.
79 *
80 * @param invocation the invocation that is being intercepted; never
81 * {@code null}
82 * @param invocationContext the context of the invocation that is being
83 * intercepted; never {@code null}
84 * @param extensionContext the current extension context; never {@code null}
85 * @throws Throwable in case of failures
86 */
87 default void interceptBeforeAllMethod(Invocation<Void> invocation,
88 ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
89 invocation.proceed();
90 }
91
92 /**
93 * Intercept the invocation of a {@link BeforeEach @BeforeEach} method.
94 *
95 * @param invocation the invocation that is being intercepted; never
96 * {@code null}
97 * @param invocationContext the context of the invocation that is being
98 * intercepted; never {@code null}
99 * @param extensionContext the current extension context; never {@code null}
100 * @throws Throwable in case of failures
101 */
102 default void interceptBeforeEachMethod(Invocation<Void> invocation,
103 ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
104 invocation.proceed();
105 }
106
107 /**
108 * Intercept the invocation of a {@link Test @Test} method.
109 *
110 * @param invocation the invocation that is being intercepted; never
111 * {@code null}
112 * @param invocationContext the context of the invocation that is being
113 * intercepted; never {@code null}
114 * @param extensionContext the current extension context; never {@code null}
115 * @throws Throwable in case of failures
116 */
117 default void interceptTestMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext,
118 ExtensionContext extensionContext) throws Throwable {
119 invocation.proceed();
120 }
121
122 /**
123 * Intercept the invocation of a {@link TestFactory @TestFactory} method,
124 * such as a {@link org.junit.jupiter.api.RepeatedTest @RepeatedTest} or
125 * {@code @ParameterizedTest} method.
126 *
127 * @param invocation the invocation that is being intercepted; never
128 * {@code null}
129 * @param invocationContext the context of the invocation that is being
130 * intercepted; never {@code null}
131 * @param extensionContext the current extension context; never {@code null}
132 * @param <T> the result type
133 * @return the result of the invocation; potentially {@code null}
134 * @throws Throwable in case of failures
135 */
136 default <T> T interceptTestFactoryMethod(Invocation<T> invocation,
137 ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
138 return invocation.proceed();
139 }
140
141 /**
142 * Intercept the invocation of a {@link TestTemplate @TestTemplate} method.
143 *
144 * @param invocation the invocation that is being intercepted; never
145 * {@code null}
146 * @param invocationContext the context of the invocation that is being
147 * intercepted; never {@code null}
148 * @param extensionContext the current extension context; never {@code null}
149 * @throws Throwable in case of failures
150 */
151 default void interceptTestTemplateMethod(Invocation<Void> invocation,
152 ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
153 invocation.proceed();
154 }
155
156 /**
157 * Intercept the invocation of a {@link DynamicTest}.
158 *
159 * @param invocation the invocation that is being intercepted; never
160 * {@code null}
161 * @param extensionContext the current extension context; never {@code null}
162 * @throws Throwable in case of failures
163 * @deprecated use {@link #interceptDynamicTest(Invocation, DynamicTestInvocationContext, ExtensionContext)} instead
164 */
165 @Deprecated
166 @API(status = DEPRECATED, since = "5.8")
167 default void interceptDynamicTest(Invocation<Void> invocation, ExtensionContext extensionContext) throws Throwable {
168 invocation.proceed();
169 }
170
171 /**
172 * Intercept the invocation of a {@link DynamicTest}.
173 *
174 * @param invocation the invocation that is being intercepted; never
175 * {@code null}
176 * @param invocationContext the context of the invocation that is being
177 * intercepted; never {@code null}
178 * @param extensionContext the current extension context; never {@code null}
179 * @throws Throwable in case of failures
180 */
181 @API(status = EXPERIMENTAL, since = "5.8")
182 default void interceptDynamicTest(Invocation<Void> invocation, DynamicTestInvocationContext invocationContext,
183 ExtensionContext extensionContext) throws Throwable {
184 // by default call the old interceptDynamicTest(Invocation, ExtensionContext) method so that existing extensions still work
185 interceptDynamicTest(invocation, extensionContext);
186 }
187
188 /**
189 * Intercept the invocation of an {@link AfterEach @AfterEach} method.
190 *
191 * @param invocation the invocation that is being intercepted; never
192 * {@code null}
193 * @param invocationContext the context of the invocation that is being
194 * intercepted; never {@code null}
195 * @param extensionContext the current extension context; never {@code null}
196 * @throws Throwable in case of failures
197 */
198 default void interceptAfterEachMethod(Invocation<Void> invocation,
199 ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
200 invocation.proceed();
201 }
202
203 /**
204 * Intercept the invocation of an {@link AfterAll @AfterAll} method.
205 *
206 * @param invocation the invocation that is being intercepted; never
207 * {@code null}
208 * @param invocationContext the context of the invocation that is being
209 * intercepted; never {@code null}
210 * @param extensionContext the current extension context; never {@code null}
211 * @throws Throwable in case of failures
212 */
213 default void interceptAfterAllMethod(Invocation<Void> invocation,
214 ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
215 invocation.proceed();
216 }
217
218 /**
219 * An invocation that returns a result and may throw a {@link Throwable}.
220 *
221 * <p>This interface is not intended to be implemented by clients.
222 *
223 * @param <T> the result type
224 * @since 5.5
225 */
226 @API(status = STABLE, since = "5.10")
227 interface Invocation<T> {
228
229 /**
230 * Proceed with this invocation.
231 *
232 * @return the result of this invocation; potentially {@code null}.
233 * @throws Throwable in case the invocation failed
234 */
235 T proceed() throws Throwable;
236
237 /**
238 * Explicitly skip this invocation.
239 *
240 * <p>This allows to bypass the check that {@link #proceed()} must be
241 * called at least once. The default implementation does nothing.
242 */
243 @API(status = STABLE, since = "5.10")
244 default void skip() {
245 // do nothing
246 }
247 }
248
249 }