Skip to content

Commit 14ec8d7

Browse files
committed
Introduce config param for default test instance lifecycle
WIP Issue: #905
1 parent 6316bd0 commit 14ec8d7

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

junit-jupiter-api/src/main/java/org/junit/jupiter/api/TestInstance.java

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757

5858
/**
5959
* Enumeration of test instance lifecycle <em>modes</em>.
60+
*
61+
* @see #PER_METHOD
62+
* @see #PER_CLASS
6063
*/
6164
enum Lifecycle {
6265

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/Constants.java

+15
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ public final class Constants {
7070
*/
7171
public static final String EXTENSIONS_AUTODETECTION_ENABLED_PROPERTY_NAME = "junit.jupiter.extensions.autodetection.enabled";
7272

73+
/**
74+
* Property name used to set the default test instance lifecycle mode: {@value}
75+
*
76+
* <h3>Supported Values</h3>
77+
*
78+
* <p>Supported values include names of enum constants defined in
79+
* {@link org.junit.jupiter.api.TestInstance.Lifecycle}, ignoring case.
80+
*
81+
* <p>If not specified, the default is "per_method" which corresponds to
82+
* {@code @TestInstance(Lifecycle.PER_METHOD)}.
83+
*
84+
* @see org.junit.jupiter.api.TestInstance
85+
*/
86+
public static final String DEFAULT_TEST_INSTANCE_LIFECYCLE_PROPERTY_NAME = "junit.jupiter.testinstance.lifecycle.default";
87+
7388
private Constants() {
7489
/* no-op */
7590
}

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassTestDescriptor.java

+30-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
package org.junit.jupiter.engine.descriptor;
1212

13+
import static org.junit.jupiter.engine.Constants.DEFAULT_TEST_INSTANCE_LIFECYCLE_PROPERTY_NAME;
1314
import static org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.findAfterAllMethods;
1415
import static org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.findAfterEachMethods;
1516
import static org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.findBeforeAllMethods;
@@ -21,6 +22,7 @@
2122
import java.util.ArrayList;
2223
import java.util.Collections;
2324
import java.util.List;
25+
import java.util.Optional;
2426
import java.util.Set;
2527
import java.util.function.Function;
2628

@@ -43,6 +45,7 @@
4345
import org.junit.platform.commons.util.AnnotationUtils;
4446
import org.junit.platform.commons.util.Preconditions;
4547
import org.junit.platform.commons.util.ReflectionUtils;
48+
import org.junit.platform.engine.ConfigurationParameters;
4649
import org.junit.platform.engine.TestDescriptor;
4750
import org.junit.platform.engine.TestTag;
4851
import org.junit.platform.engine.UniqueId;
@@ -65,7 +68,8 @@ public class ClassTestDescriptor extends JupiterTestDescriptor {
6568
private static final ExecutableInvoker executableInvoker = new ExecutableInvoker();
6669

6770
private final Class<?> testClass;
68-
private final Lifecycle lifecycle;
71+
72+
private Lifecycle lifecycle;
6973

7074
private List<Method> beforeAllMethods;
7175
private List<Method> afterAllMethods;
@@ -83,7 +87,6 @@ protected ClassTestDescriptor(UniqueId uniqueId, Function<Class<?>, String> defa
8387
defaultDisplayNameGenerator), new ClassSource(testClass));
8488

8589
this.testClass = testClass;
86-
this.lifecycle = getTestInstanceLifecycle(testClass);
8790
}
8891

8992
// --- TestDescriptor ------------------------------------------------------
@@ -117,6 +120,8 @@ private static String generateDefaultDisplayName(Class<?> testClass) {
117120

118121
@Override
119122
public JupiterEngineExecutionContext prepare(JupiterEngineExecutionContext context) {
123+
this.lifecycle = getTestInstanceLifecycle(testClass, context.getConfigurationParameters());
124+
120125
this.beforeAllMethods = findBeforeAllMethods(testClass, this.lifecycle == Lifecycle.PER_METHOD);
121126
this.afterAllMethods = findAfterAllMethods(testClass, this.lifecycle == Lifecycle.PER_METHOD);
122127
this.beforeEachMethods = findBeforeEachMethods(testClass);
@@ -291,12 +296,33 @@ private void invokeMethodInExtensionContext(Method method, ExtensionContext cont
291296
executableInvoker.invoke(method, testInstance, context, registry);
292297
}
293298

294-
private static TestInstance.Lifecycle getTestInstanceLifecycle(Class<?> testClass) {
299+
private static TestInstance.Lifecycle getTestInstanceLifecycle(Class<?> testClass,
300+
ConfigurationParameters configurationParameters) {
301+
295302
// @formatter:off
296303
return AnnotationUtils.findAnnotation(testClass, TestInstance.class)
297304
.map(TestInstance::value)
298-
.orElse(Lifecycle.PER_METHOD);
305+
.orElseGet(() -> getDefaultTestInstanceLifecycle(configurationParameters));
299306
// @formatter:on
300307
}
301308

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+
302328
}

0 commit comments

Comments
 (0)