Skip to content

Commit bdfba9a

Browse files
d-williamjuliette-derancourt
authored andcommitted
Allow DisplayNameGenerator methods to return null to signal to fall back to the default generator
Fixes #2969 Closes #2974
1 parent 143beb4 commit bdfba9a

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,29 +74,35 @@ public interface DisplayNameGenerator {
7474
/**
7575
* Generate a display name for the given top-level or {@code static} nested test class.
7676
*
77+
* If {@code null} is generated, the default generator will be used to generate the display name.
78+
*
7779
* @param testClass the class to generate a name for; never {@code null}
78-
* @return the display name for the class; never {@code null} or blank
80+
* @return the display name for the class; never blank
7981
*/
8082
String generateDisplayNameForClass(Class<?> testClass);
8183

8284
/**
8385
* Generate a display name for the given {@link Nested @Nested} inner test class.
8486
*
87+
* If {@code null} is generated, the default generator will be used to generate the display name.
88+
*
8589
* @param nestedClass the class to generate a name for; never {@code null}
86-
* @return the display name for the nested class; never {@code null} or blank
90+
* @return the display name for the nested class; never blank
8791
*/
8892
String generateDisplayNameForNestedClass(Class<?> nestedClass);
8993

9094
/**
9195
* Generate a display name for the given method.
9296
*
97+
* If {@code null} is generated, the default generator will be used to generate the display name.
98+
*
9399
* @implNote The class instance supplied as {@code testClass} may differ from
94100
* the class returned by {@code testMethod.getDeclaringClass()} &mdash; for
95101
* example, when a test method is inherited from a superclass.
96102
*
97103
* @param testClass the class the test method is invoked on; never {@code null}
98104
* @param testMethod method to generate a display name for; never {@code null}
99-
* @return the display name for the test; never {@code null} or blank
105+
* @return the display name for the test; never blank
100106
*/
101107
String generateDisplayNameForMethod(Class<?> testClass, Method testMethod);
102108

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.lang.reflect.AnnotatedElement;
1616
import java.lang.reflect.Method;
1717
import java.util.Optional;
18+
import java.util.function.Function;
1819
import java.util.function.Supplier;
1920

2021
import org.junit.jupiter.api.DisplayName;
@@ -90,21 +91,36 @@ static String determineDisplayName(AnnotatedElement element, Supplier<String> di
9091

9192
static String determineDisplayNameForMethod(Class<?> testClass, Method testMethod,
9293
JupiterConfiguration configuration) {
93-
DisplayNameGenerator generator = getDisplayNameGenerator(testClass, configuration);
94-
return determineDisplayName(testMethod, () -> generator.generateDisplayNameForMethod(testClass, testMethod));
94+
return determineDisplayName(testMethod,
95+
createDisplayNameSupplierForMethod(testClass, testMethod, configuration));
9596
}
9697

9798
static Supplier<String> createDisplayNameSupplierForClass(Class<?> testClass, JupiterConfiguration configuration) {
98-
return () -> getDisplayNameGenerator(testClass, configuration).generateDisplayNameForClass(testClass);
99+
return createDisplayNameSupplier(testClass, configuration,
100+
generator -> generator.generateDisplayNameForClass(testClass));
99101
}
100102

101103
static Supplier<String> createDisplayNameSupplierForNestedClass(Class<?> testClass,
102104
JupiterConfiguration configuration) {
103-
return () -> getDisplayNameGenerator(testClass, configuration).generateDisplayNameForNestedClass(testClass);
105+
return createDisplayNameSupplier(testClass, configuration,
106+
generator -> generator.generateDisplayNameForNestedClass(testClass));
104107
}
105108

106-
private static DisplayNameGenerator getDisplayNameGenerator(Class<?> testClass,
109+
static Supplier<String> createDisplayNameSupplierForMethod(Class<?> testClass, Method testMethod,
107110
JupiterConfiguration configuration) {
111+
return createDisplayNameSupplier(testClass, configuration,
112+
generator -> generator.generateDisplayNameForMethod(testClass, testMethod));
113+
}
114+
115+
private static Supplier<String> createDisplayNameSupplier(Class<?> testClass, JupiterConfiguration configuration,
116+
Function<DisplayNameGenerator, String> generatorFunction) {
117+
Preconditions.notNull(generatorFunction, "DisplayNameGenerator function must not be null");
118+
return () -> getOptionalDisplayNameGenerator(testClass) //
119+
.map(generatorFunction) //
120+
.orElseGet(() -> generatorFunction.apply(configuration.getDefaultDisplayNameGenerator()));
121+
}
122+
123+
private static Optional<DisplayNameGenerator> getOptionalDisplayNameGenerator(Class<?> testClass) {
108124
Preconditions.notNull(testClass, "Test class must not be null");
109125

110126
return AnnotationUtils.findAnnotation(testClass, DisplayNameGeneration.class, true) //
@@ -123,8 +139,7 @@ private static DisplayNameGenerator getDisplayNameGenerator(Class<?> testClass,
123139
return indicativeSentencesGenerator;
124140
}
125141
return ReflectionUtils.newInstance(displayNameGeneratorClass);
126-
}) //
127-
.orElseGet(configuration::getDefaultDisplayNameGenerator);
142+
});
128143
}
129144

130145
}

0 commit comments

Comments
 (0)