Skip to content

Commit 4ee85e2

Browse files
Polish and add tests for PR #2974
1 parent bdfba9a commit 4ee85e2

File tree

3 files changed

+67
-7
lines changed

3 files changed

+67
-7
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ 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.
77+
* <p>If it returns {@code null}, the default display name generator will be used instead.
7878
*
7979
* @param testClass the class to generate a name for; never {@code null}
8080
* @return the display name for the class; never blank
@@ -84,7 +84,7 @@ public interface DisplayNameGenerator {
8484
/**
8585
* Generate a display name for the given {@link Nested @Nested} inner test class.
8686
*
87-
* If {@code null} is generated, the default generator will be used to generate the display name.
87+
* <p>If it returns {@code null}, the default display name generator will be used instead.
8888
*
8989
* @param nestedClass the class to generate a name for; never {@code null}
9090
* @return the display name for the nested class; never blank
@@ -94,7 +94,7 @@ public interface DisplayNameGenerator {
9494
/**
9595
* Generate a display name for the given method.
9696
*
97-
* If {@code null} is generated, the default generator will be used to generate the display name.
97+
* <p>If it returns {@code null}, the default display name generator will be used instead.
9898
*
9999
* @implNote The class instance supplied as {@code testClass} may differ from
100100
* the class returned by {@code testMethod.getDeclaringClass()} &mdash; for

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,20 @@ static Supplier<String> createDisplayNameSupplierForNestedClass(Class<?> testCla
106106
generator -> generator.generateDisplayNameForNestedClass(testClass));
107107
}
108108

109-
static Supplier<String> createDisplayNameSupplierForMethod(Class<?> testClass, Method testMethod,
109+
private static Supplier<String> createDisplayNameSupplierForMethod(Class<?> testClass, Method testMethod,
110110
JupiterConfiguration configuration) {
111111
return createDisplayNameSupplier(testClass, configuration,
112112
generator -> generator.generateDisplayNameForMethod(testClass, testMethod));
113113
}
114114

115115
private static Supplier<String> createDisplayNameSupplier(Class<?> testClass, JupiterConfiguration configuration,
116116
Function<DisplayNameGenerator, String> generatorFunction) {
117-
Preconditions.notNull(generatorFunction, "DisplayNameGenerator function must not be null");
118-
return () -> getOptionalDisplayNameGenerator(testClass) //
117+
return () -> findDisplayNameGenerator(testClass) //
119118
.map(generatorFunction) //
120119
.orElseGet(() -> generatorFunction.apply(configuration.getDefaultDisplayNameGenerator()));
121120
}
122121

123-
private static Optional<DisplayNameGenerator> getOptionalDisplayNameGenerator(Class<?> testClass) {
122+
private static Optional<DisplayNameGenerator> findDisplayNameGenerator(Class<?> testClass) {
124123
Preconditions.notNull(testClass, "Test class must not be null");
125124

126125
return AnnotationUtils.findAnnotation(testClass, DisplayNameGeneration.class, true) //

junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/DisplayNameUtilsTests.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ void shouldGetDisplayNameFromDefaultDisplayNameGenerator() {
101101

102102
assertThat(displayName.get()).isEqualTo("class-display-name");
103103
}
104+
105+
@Test
106+
void shouldFallbackOnDefaultDisplayNameGeneratorWhenNullIsGenerated() {
107+
when(configuration.getDefaultDisplayNameGenerator()).thenReturn(new CustomDisplayNameGenerator());
108+
Supplier<String> displayName = DisplayNameUtils.createDisplayNameSupplierForClass(
109+
NullDisplayNameTestCase.class, configuration);
110+
111+
assertThat(displayName.get()).isEqualTo("class-display-name");
112+
}
104113
}
105114
}
106115

@@ -126,6 +135,15 @@ void shouldGetDisplayNameFromDefaultDisplayNameGenerator() {
126135

127136
assertThat(displayName.get()).isEqualTo("nested-class-display-name");
128137
}
138+
139+
@Test
140+
void shouldFallbackOnDefaultDisplayNameGeneratorWhenNullIsGenerated() {
141+
when(configuration.getDefaultDisplayNameGenerator()).thenReturn(new CustomDisplayNameGenerator());
142+
Supplier<String> displayName = DisplayNameUtils.createDisplayNameSupplierForNestedClass(
143+
NullDisplayNameTestCase.NestedTestCase.class, configuration);
144+
145+
assertThat(displayName.get()).isEqualTo("nested-class-display-name");
146+
}
129147
}
130148

131149
@Nested
@@ -153,6 +171,17 @@ void shouldGetDisplayNameFromDefaultNameGenerator() throws Exception {
153171

154172
assertThat(displayName).isEqualTo("method-display-name");
155173
}
174+
175+
@Test
176+
void shouldFallbackOnDefaultDisplayNameGeneratorWhenNullIsGenerated() throws Exception {
177+
Method method = NullDisplayNameTestCase.class.getDeclaredMethod("test");
178+
when(configuration.getDefaultDisplayNameGenerator()).thenReturn(new CustomDisplayNameGenerator());
179+
180+
String displayName = DisplayNameUtils.determineDisplayNameForMethod(NullDisplayNameTestCase.class, method,
181+
configuration);
182+
183+
assertThat(displayName).isEqualTo("method-display-name");
184+
}
156185
}
157186

158187
private LogRecord firstWarningLogRecord(LogRecordListener listener) throws AssertionError {
@@ -188,4 +217,36 @@ static class NotDisplayNameTestCase {
188217
class NestedTestCase {
189218
}
190219

220+
@DisplayNameGeneration(value = NullDisplayNameGenerator.class)
221+
static class NullDisplayNameTestCase {
222+
223+
@Test
224+
void test() {
225+
}
226+
227+
@Nested
228+
class NestedTestCase {
229+
}
230+
231+
}
232+
233+
static class NullDisplayNameGenerator implements DisplayNameGenerator {
234+
235+
@Override
236+
public String generateDisplayNameForClass(Class<?> testClass) {
237+
return null;
238+
}
239+
240+
@Override
241+
public String generateDisplayNameForNestedClass(Class<?> nestedClass) {
242+
return null;
243+
}
244+
245+
@Override
246+
public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod) {
247+
return null;
248+
}
249+
250+
}
251+
191252
}

0 commit comments

Comments
 (0)