14
14
import static org .junit .jupiter .engine .descriptor .LifecycleMethodUtils .findAfterEachMethods ;
15
15
import static org .junit .jupiter .engine .descriptor .LifecycleMethodUtils .findBeforeAllMethods ;
16
16
import static org .junit .jupiter .engine .descriptor .LifecycleMethodUtils .findBeforeEachMethods ;
17
+ import static org .junit .jupiter .engine .descriptor .TestInstanceLifecycleUtils .getTestInstanceLifecycle ;
17
18
import static org .junit .platform .commons .meta .API .Usage .Internal ;
18
19
19
20
import java .lang .reflect .Constructor ;
24
25
import java .util .Set ;
25
26
import java .util .function .Function ;
26
27
27
- import org .junit .jupiter .api .TestInstance ;
28
28
import org .junit .jupiter .api .TestInstance .Lifecycle ;
29
29
import org .junit .jupiter .api .extension .AfterAllCallback ;
30
30
import org .junit .jupiter .api .extension .BeforeAllCallback ;
40
40
import org .junit .jupiter .engine .extension .ExtensionRegistry ;
41
41
import org .junit .platform .commons .JUnitException ;
42
42
import org .junit .platform .commons .meta .API ;
43
- import org .junit .platform .commons .util .AnnotationUtils ;
44
43
import org .junit .platform .commons .util .Preconditions ;
45
44
import org .junit .platform .commons .util .ReflectionUtils ;
46
45
import org .junit .platform .engine .TestDescriptor ;
@@ -65,7 +64,6 @@ public class ClassTestDescriptor extends JupiterTestDescriptor {
65
64
private static final ExecutableInvoker executableInvoker = new ExecutableInvoker ();
66
65
67
66
private final Class <?> testClass ;
68
- private final Lifecycle lifecycle ;
69
67
70
68
private List <Method > beforeAllMethods ;
71
69
private List <Method > afterAllMethods ;
@@ -83,7 +81,6 @@ protected ClassTestDescriptor(UniqueId uniqueId, Function<Class<?>, String> defa
83
81
defaultDisplayNameGenerator ), new ClassSource (testClass ));
84
82
85
83
this .testClass = testClass ;
86
- this .lifecycle = getTestInstanceLifecycle (testClass );
87
84
}
88
85
89
86
// --- TestDescriptor ------------------------------------------------------
@@ -117,8 +114,10 @@ private static String generateDefaultDisplayName(Class<?> testClass) {
117
114
118
115
@ Override
119
116
public JupiterEngineExecutionContext prepare (JupiterEngineExecutionContext context ) {
120
- this .beforeAllMethods = findBeforeAllMethods (testClass , this .lifecycle == Lifecycle .PER_METHOD );
121
- this .afterAllMethods = findAfterAllMethods (testClass , this .lifecycle == Lifecycle .PER_METHOD );
117
+ Lifecycle lifecycle = getTestInstanceLifecycle (testClass , context .getConfigurationParameters ());
118
+
119
+ this .beforeAllMethods = findBeforeAllMethods (testClass , lifecycle == Lifecycle .PER_METHOD );
120
+ this .afterAllMethods = findAfterAllMethods (testClass , lifecycle == Lifecycle .PER_METHOD );
122
121
this .beforeEachMethods = findBeforeEachMethods (testClass );
123
122
this .afterEachMethods = findAfterEachMethods (testClass );
124
123
@@ -134,7 +133,7 @@ public JupiterEngineExecutionContext prepare(JupiterEngineExecutionContext conte
134
133
135
134
// @formatter:off
136
135
return context .extend ()
137
- .withTestInstanceProvider (testInstanceProvider (context , registry , extensionContext ))
136
+ .withTestInstanceProvider (testInstanceProvider (context , registry , extensionContext , lifecycle ))
138
137
.withExtensionRegistry (registry )
139
138
.withExtensionContext (extensionContext )
140
139
.withThrowableCollector (throwableCollector )
@@ -168,9 +167,9 @@ public void after(JupiterEngineExecutionContext context) throws Exception {
168
167
}
169
168
170
169
private TestInstanceProvider testInstanceProvider (JupiterEngineExecutionContext parentExecutionContext ,
171
- ExtensionRegistry registry , ClassExtensionContext extensionContext ) {
170
+ ExtensionRegistry registry , ClassExtensionContext extensionContext , Lifecycle lifecycle ) {
172
171
173
- if (this . lifecycle == Lifecycle .PER_CLASS ) {
172
+ if (lifecycle == Lifecycle .PER_CLASS ) {
174
173
// Eagerly load test instance for BeforeAllCallbacks, if necessary,
175
174
// and store the instance in the ExtensionContext.
176
175
Object instance = instantiateAndPostProcessTestInstance (parentExecutionContext , extensionContext , registry );
@@ -283,20 +282,11 @@ private AfterEachMethodAdapter synthesizeAfterEachMethodAdapter(Method method) {
283
282
}
284
283
285
284
private void invokeMethodInExtensionContext (Method method , ExtensionContext context , ExtensionRegistry registry ) {
286
-
287
285
Object testInstance = context .getRequiredTestInstance ();
288
286
testInstance = ReflectionUtils .getOutermostInstance (testInstance , method .getDeclaringClass ()).orElseThrow (
289
287
() -> new JUnitException ("Failed to find instance for method: " + method .toGenericString ()));
290
288
291
289
executableInvoker .invoke (method , testInstance , context , registry );
292
290
}
293
291
294
- private static TestInstance .Lifecycle getTestInstanceLifecycle (Class <?> testClass ) {
295
- // @formatter:off
296
- return AnnotationUtils .findAnnotation (testClass , TestInstance .class )
297
- .map (TestInstance ::value )
298
- .orElse (Lifecycle .PER_METHOD );
299
- // @formatter:on
300
- }
301
-
302
292
}
0 commit comments