10
10
11
11
package org .junit .jupiter .engine .descriptor ;
12
12
13
+ import static org .junit .jupiter .engine .Constants .DEFAULT_TEST_INSTANCE_LIFECYCLE_PROPERTY_NAME ;
13
14
import static org .junit .jupiter .engine .descriptor .LifecycleMethodUtils .findAfterAllMethods ;
14
15
import static org .junit .jupiter .engine .descriptor .LifecycleMethodUtils .findAfterEachMethods ;
15
16
import static org .junit .jupiter .engine .descriptor .LifecycleMethodUtils .findBeforeAllMethods ;
21
22
import java .util .ArrayList ;
22
23
import java .util .Collections ;
23
24
import java .util .List ;
25
+ import java .util .Optional ;
24
26
import java .util .Set ;
25
27
import java .util .function .Function ;
26
28
43
45
import org .junit .platform .commons .util .AnnotationUtils ;
44
46
import org .junit .platform .commons .util .Preconditions ;
45
47
import org .junit .platform .commons .util .ReflectionUtils ;
48
+ import org .junit .platform .engine .ConfigurationParameters ;
46
49
import org .junit .platform .engine .TestDescriptor ;
47
50
import org .junit .platform .engine .TestTag ;
48
51
import org .junit .platform .engine .UniqueId ;
@@ -65,7 +68,8 @@ public class ClassTestDescriptor extends JupiterTestDescriptor {
65
68
private static final ExecutableInvoker executableInvoker = new ExecutableInvoker ();
66
69
67
70
private final Class <?> testClass ;
68
- private final Lifecycle lifecycle ;
71
+
72
+ private Lifecycle lifecycle ;
69
73
70
74
private List <Method > beforeAllMethods ;
71
75
private List <Method > afterAllMethods ;
@@ -83,7 +87,6 @@ protected ClassTestDescriptor(UniqueId uniqueId, Function<Class<?>, String> defa
83
87
defaultDisplayNameGenerator ), new ClassSource (testClass ));
84
88
85
89
this .testClass = testClass ;
86
- this .lifecycle = getTestInstanceLifecycle (testClass );
87
90
}
88
91
89
92
// --- TestDescriptor ------------------------------------------------------
@@ -117,6 +120,8 @@ private static String generateDefaultDisplayName(Class<?> testClass) {
117
120
118
121
@ Override
119
122
public JupiterEngineExecutionContext prepare (JupiterEngineExecutionContext context ) {
123
+ this .lifecycle = getTestInstanceLifecycle (testClass , context .getConfigurationParameters ());
124
+
120
125
this .beforeAllMethods = findBeforeAllMethods (testClass , this .lifecycle == Lifecycle .PER_METHOD );
121
126
this .afterAllMethods = findAfterAllMethods (testClass , this .lifecycle == Lifecycle .PER_METHOD );
122
127
this .beforeEachMethods = findBeforeEachMethods (testClass );
@@ -291,12 +296,33 @@ private void invokeMethodInExtensionContext(Method method, ExtensionContext cont
291
296
executableInvoker .invoke (method , testInstance , context , registry );
292
297
}
293
298
294
- private static TestInstance .Lifecycle getTestInstanceLifecycle (Class <?> testClass ) {
299
+ private static TestInstance .Lifecycle getTestInstanceLifecycle (Class <?> testClass ,
300
+ ConfigurationParameters configurationParameters ) {
301
+
295
302
// @formatter:off
296
303
return AnnotationUtils .findAnnotation (testClass , TestInstance .class )
297
304
.map (TestInstance ::value )
298
- .orElse ( Lifecycle . PER_METHOD );
305
+ .orElseGet (() -> getDefaultTestInstanceLifecycle ( configurationParameters ) );
299
306
// @formatter:on
300
307
}
301
308
309
+ private static TestInstance .Lifecycle getDefaultTestInstanceLifecycle (
310
+ ConfigurationParameters configurationParameters ) {
311
+
312
+ Optional <String > optional = configurationParameters .get (DEFAULT_TEST_INSTANCE_LIFECYCLE_PROPERTY_NAME );
313
+ if (optional .isPresent ()) {
314
+ try {
315
+ String constantName = optional .get ().trim ().toUpperCase ();
316
+ Lifecycle lifecycle = TestInstance .Lifecycle .valueOf (constantName );
317
+ // TODO Log info message.
318
+ return lifecycle ;
319
+ }
320
+ catch (Exception ex ) {
321
+ // TODO Log error message.
322
+ }
323
+ }
324
+
325
+ return Lifecycle .PER_METHOD ;
326
+ }
327
+
302
328
}
0 commit comments