15
15
import java .lang .reflect .AnnotatedElement ;
16
16
import java .lang .reflect .Method ;
17
17
import java .util .Optional ;
18
+ import java .util .function .Function ;
18
19
import java .util .function .Supplier ;
19
20
20
21
import org .junit .jupiter .api .DisplayName ;
@@ -90,21 +91,36 @@ static String determineDisplayName(AnnotatedElement element, Supplier<String> di
90
91
91
92
static String determineDisplayNameForMethod (Class <?> testClass , Method testMethod ,
92
93
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 ));
95
96
}
96
97
97
98
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 ));
99
101
}
100
102
101
103
static Supplier <String > createDisplayNameSupplierForNestedClass (Class <?> testClass ,
102
104
JupiterConfiguration configuration ) {
103
- return () -> getDisplayNameGenerator (testClass , configuration ).generateDisplayNameForNestedClass (testClass );
105
+ return createDisplayNameSupplier (testClass , configuration ,
106
+ generator -> generator .generateDisplayNameForNestedClass (testClass ));
104
107
}
105
108
106
- private static DisplayNameGenerator getDisplayNameGenerator (Class <?> testClass ,
109
+ static Supplier < String > createDisplayNameSupplierForMethod (Class <?> testClass , Method testMethod ,
107
110
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 ) {
108
124
Preconditions .notNull (testClass , "Test class must not be null" );
109
125
110
126
return AnnotationUtils .findAnnotation (testClass , DisplayNameGeneration .class , true ) //
@@ -123,8 +139,7 @@ private static DisplayNameGenerator getDisplayNameGenerator(Class<?> testClass,
123
139
return indicativeSentencesGenerator ;
124
140
}
125
141
return ReflectionUtils .newInstance (displayNameGeneratorClass );
126
- }) //
127
- .orElseGet (configuration ::getDefaultDisplayNameGenerator );
142
+ });
128
143
}
129
144
130
145
}
0 commit comments