@DisplayName
is a great way to write meaning full human-readable text to distinguish the test case methods. @DisplayName
helps to read test case report better.
but if an application has lots of test cases, then writing @DisplayName
for each method is tedious work.
Junit 5 provides @DisplayNameGeneration
annotation, which will automatically generate the test case method name for the test case generated report using the method name.
Junit 5 has DisplayNameGenerator
interface, which defines 3 methods
generateDisplayNameForClass
Read the class name and use them for Display Name
generateDisplayNameForNestedClass
Read the nested class name and use them for Display Name
parameterTypesAsString
Read the input parameters of test case methods and use them for Display Name.
The
@DisplayName
will always take precedence over@DisplayNameGenerator
.
Predefined Display Name Generator
Junit 5 provides some predefined DisplayNameGenerator
interface implementations.
Standard
Standard
is the default Display Name Generator for Junit 5.
Simple
Simple
will remove the trailing parentheses of methods with no parameter.
ReplaceUnderscores
ReplaceUnderscores
will replace all underscores with space.
IndicativeSentences
IndicativeSentences
creates Display Name by concatenating the name of the Test class and test case method name.
Display Name Generator Configuration Parameter
Annotating multiple test cases class with @DisplayNameGeneration is redundant, Junit 5 allows to define of the fully qualified name of Display Name Generator in src/test/resources/junit-platform.properties
file.
Standard Display Name Generator
junit.jupiter.displayname.generator.default=org.junit.jupiter.api.DisplayNameGenerator$Standard
Simple Display Name Generator
junit.jupiter.displayname.generator.default=org.junit.jupiter.api.DisplayNameGenerator$Simple
Replace Underscores Display Name Generator
junit.jupiter.displayname.generator.default=org.junit.jupiter.api.DisplayNameGenerator$ReplaceUnderscores
IndicativeSentences Display Name Generator
junit.jupiter.displayname.generator.default=org.junit.jupiter.api.DisplayNameGenerator$IndicativeSentences
Custom Display Name Generator
junit.jupiter.displayname.generator.default=org.wesome.junit5.CustomDisplayNameGenerator