@@ -68,6 +68,7 @@ public class ClassTestDescriptor extends JupiterTestDescriptor {
68
68
private static final ExecutableInvoker executableInvoker = new ExecutableInvoker ();
69
69
70
70
private final Class <?> testClass ;
71
+ private final Lifecycle lifecycle ;
71
72
72
73
private final List <Method > beforeAllMethods ;
73
74
private final List <Method > afterAllMethods ;
@@ -85,9 +86,10 @@ protected ClassTestDescriptor(UniqueId uniqueId, Function<Class<?>, String> defa
85
86
defaultDisplayNameGenerator ));
86
87
87
88
this .testClass = testClass ;
89
+ this .lifecycle = getTestInstanceLifecycle (testClass );
88
90
89
- this .beforeAllMethods = findBeforeAllMethods (testClass );
90
- this .afterAllMethods = findAfterAllMethods (testClass );
91
+ this .beforeAllMethods = findBeforeAllMethods (testClass , this . lifecycle == Lifecycle . PER_METHOD );
92
+ this .afterAllMethods = findAfterAllMethods (testClass , this . lifecycle == Lifecycle . PER_METHOD );
91
93
this .beforeEachMethods = findBeforeEachMethods (testClass );
92
94
this .afterEachMethods = findAfterEachMethods (testClass );
93
95
@@ -204,9 +206,11 @@ private void invokeBeforeAllMethods(JupiterEngineExecutionContext context) {
204
206
ExtensionRegistry registry = context .getExtensionRegistry ();
205
207
ContainerExtensionContext extensionContext = (ContainerExtensionContext ) context .getExtensionContext ();
206
208
ThrowableCollector throwableCollector = context .getThrowableCollector ();
209
+ Object testInstance = getTestInstanceForClassLevelCallbacks (context );
207
210
208
211
for (Method method : this .beforeAllMethods ) {
209
- throwableCollector .execute (() -> executableInvoker .invoke (method , extensionContext , registry ));
212
+ throwableCollector .execute (
213
+ () -> executableInvoker .invoke (method , testInstance , extensionContext , registry ));
210
214
if (throwableCollector .isNotEmpty ()) {
211
215
break ;
212
216
}
@@ -217,9 +221,15 @@ private void invokeAfterAllMethods(JupiterEngineExecutionContext context) {
217
221
ExtensionRegistry registry = context .getExtensionRegistry ();
218
222
ContainerExtensionContext extensionContext = (ContainerExtensionContext ) context .getExtensionContext ();
219
223
ThrowableCollector throwableCollector = context .getThrowableCollector ();
224
+ Object testInstance = getTestInstanceForClassLevelCallbacks (context );
220
225
221
- this .afterAllMethods .forEach (
222
- method -> throwableCollector .execute (() -> executableInvoker .invoke (method , extensionContext , registry )));
226
+ this .afterAllMethods .forEach (method -> throwableCollector .execute (
227
+ () -> executableInvoker .invoke (method , testInstance , extensionContext , registry )));
228
+ }
229
+
230
+ private Object getTestInstanceForClassLevelCallbacks (JupiterEngineExecutionContext context ) {
231
+ return this .lifecycle == Lifecycle .PER_CLASS
232
+ ? context .getTestInstanceProvider ().getTestInstance (Optional .empty ()) : null ;
223
233
}
224
234
225
235
private void invokeAfterAllCallbacks (JupiterEngineExecutionContext context ) {
@@ -274,7 +284,6 @@ private void invokeMethodInTestExtensionContext(Method method, TestExtensionCont
274
284
private final class LifecycleAwareTestInstanceProvider implements TestInstanceProvider {
275
285
276
286
private final Class <?> testClass ;
277
- private final Lifecycle lifecycle ;
278
287
private final ExtensionRegistry registry ;
279
288
private final ExtensionContext extensionContext ;
280
289
private Object testInstance ;
@@ -283,14 +292,13 @@ private final class LifecycleAwareTestInstanceProvider implements TestInstancePr
283
292
ExtensionContext extensionContext ) {
284
293
285
294
this .testClass = testClass ;
286
- this .lifecycle = getInstanceLifecycle (testClass );
287
295
this .registry = registry ;
288
296
this .extensionContext = extensionContext ;
289
297
}
290
298
291
299
@ Override
292
- public Object getTestInstance (Optional <ExtensionRegistry > childExtensionRegistry ) throws Exception {
293
- if (this .lifecycle == Lifecycle .PER_METHOD ) {
300
+ public Object getTestInstance (Optional <ExtensionRegistry > childExtensionRegistry ) {
301
+ if (ClassTestDescriptor . this .lifecycle == Lifecycle .PER_METHOD ) {
294
302
return createTestInstance (childExtensionRegistry );
295
303
}
296
304
@@ -310,14 +318,14 @@ private Object createTestInstance(Optional<ExtensionRegistry> childExtensionRegi
310
318
return instance ;
311
319
}
312
320
313
- private TestInstance .Lifecycle getInstanceLifecycle (Class <?> testClass ) {
314
- // @formatter:off
315
- return AnnotationUtils .findAnnotation (testClass , TestInstance .class )
316
- .map (TestInstance ::value )
317
- .orElse (Lifecycle .PER_METHOD );
318
- // @formatter:on
319
- }
321
+ }
320
322
323
+ private static TestInstance .Lifecycle getTestInstanceLifecycle (Class <?> testClass ) {
324
+ // @formatter:off
325
+ return AnnotationUtils .findAnnotation (testClass , TestInstance .class )
326
+ .map (TestInstance ::value )
327
+ .orElse (Lifecycle .PER_METHOD );
328
+ // @formatter:on
321
329
}
322
330
323
331
}
0 commit comments