Top Description Inners Fields Methods
org.junit.jupiter.api

public Interface DisplayNameGenerator

Known Direct Implementers
org.junit.jupiter.api.DisplayNameGenerator.Standard, org.junit.jupiter.api.DisplayNameGenerator.IndicativeSentences
Annotations
@API
status:STABLE
since:5.7
Static Imports
org.apiguardian.api.API.Status.STABLE, org.junit.platform.commons.support.ModifierSupport.isStatic, org.junit.platform.commons.util.AnnotationUtils.findAnnotation

DisplayNameGenerator defines the SPI for generating display names programmatically.

Display names are typically used for test reporting in IDEs and build tools and may contain spaces, special characters, and even emoji.

Concrete implementations must have a default constructor.

A DisplayNameGenerator can be configured globally for the entire test suite via the junit.jupiter.displayname.generator.default configuration parameter (see the User Guide for details) or locally for a test class via the @DisplayNameGeneration annotation.

Built-in Implementations

Since
5.4
See Also
@DisplayName, @DisplayNameGeneration

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
DisplayNameGenerator.IndicativeSentences

DisplayNameGenerator that generates complete sentences.

public static class
DisplayNameGenerator.ReplaceUnderscores

DisplayNameGenerator that replaces underscores with spaces.

public static class
DisplayNameGenerator.Simple

Simple DisplayNameGenerator that removes trailing parentheses for methods with no parameters.

public static class
DisplayNameGenerator.Standard

Standard DisplayNameGenerator.

Field Summary

Modifier and TypeField and Description
public static final String
DEFAULT_GENERATOR_PROPERTY_NAME

Property name used to set the default display name generator class name: junit.jupiter.displayname.generator.default Supported Values

Supported values include fully qualified class names for types that implement DisplayNameGenerator.

Method Summary

Modifier and TypeMethod and Description
public String

Returns:

the display name for the class; never blank
generateDisplayNameForClass
(Class<?>
the class to generate a name for; never null
testClass
)

Generate a display name for the given top-level or static nested test class.

public String

Returns:

the display name for the test; never blank
generateDisplayNameForMethod
(Class<?>
the class the test method is invoked on; never null
testClass
,
Method
method to generate a display name for; never null
testMethod
)

Generate a display name for the given method.

public String

Returns:

the display name for the nested class; never blank
generateDisplayNameForNestedClass
(Class<?>
the class to generate a name for; never null
nestedClass
)

Generate a display name for the given @Nested inner test class.

public static DisplayNameGenerator

Returns:

a DisplayNameGenerator implementation instance
getDisplayNameGenerator
(Class<?>
the generator's Class; never null, has to be a DisplayNameGenerator implementation
generatorClass
)

Return the DisplayNameGenerator instance corresponding to the given Class.

public static String

Returns:

a string representation of all parameter types of the supplied method or "()" if the method declares no parameters
parameterTypesAsString
(Method
the method from to extract the parameter types from; never null
method
)

Generate a string representation of the formal parameters of the supplied method, consisting of the simple names of the parameter types, separated by commas, and enclosed in parentheses.

Field Detail

DEFAULT_GENERATOR_PROPERTY_NAMEback to summary
public static final String DEFAULT_GENERATOR_PROPERTY_NAME

Property name used to set the default display name generator class name: junit.jupiter.displayname.generator.default

Supported Values

Supported values include fully qualified class names for types that implement DisplayNameGenerator.

If not specified, the default is DisplayNameGenerator.Standard.

Annotations
@API
status:STABLE
since:5.9
Since
5.5

Method Detail

generateDisplayNameForClassback to summary
public String generateDisplayNameForClass(Class<?> testClass)

Generate a display name for the given top-level or static nested test class.

If it returns null, the default display name generator will be used instead.

Parameters
testClass:Class<?>

the class to generate a name for; never null

Returns:String

the display name for the class; never blank

generateDisplayNameForMethodback to summary
public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod)

Generate a display name for the given method.

If it returns null, the default display name generator will be used instead.

Implementation Note

The class instance supplied as testClass may differ from the class returned by testMethod.getDeclaringClass() — for example, when a test method is inherited from a superclass.

Parameters
testClass:Class<?>

the class the test method is invoked on; never null

testMethod:Method

method to generate a display name for; never null

Returns:String

the display name for the test; never blank

generateDisplayNameForNestedClassback to summary
public String generateDisplayNameForNestedClass(Class<?> nestedClass)

Generate a display name for the given @Nested inner test class.

If it returns null, the default display name generator will be used instead.

Parameters
nestedClass:Class<?>

the class to generate a name for; never null

Returns:String

the display name for the nested class; never blank

getDisplayNameGeneratorback to summary
public static DisplayNameGenerator getDisplayNameGenerator(Class<?> generatorClass)

Return the DisplayNameGenerator instance corresponding to the given Class.

Parameters
generatorClass:Class<?>

the generator's Class; never null, has to be a DisplayNameGenerator implementation

Returns:DisplayNameGenerator

a DisplayNameGenerator implementation instance

parameterTypesAsStringback to summary
public static String parameterTypesAsString(Method method)

Generate a string representation of the formal parameters of the supplied method, consisting of the simple names of the parameter types, separated by commas, and enclosed in parentheses.

Parameters
method:Method

the method from to extract the parameter types from; never null

Returns:String

a string representation of all parameter types of the supplied method or "()" if the method declares no parameters

org.junit.jupiter.api back to summary

public Class DisplayNameGenerator.IndicativeSentences

extends Object
implements DisplayNameGenerator
Class Inheritance
  • java.lang.Object
  • org.junit.jupiter.api.DisplayNameGenerator.IndicativeSentences
All Implemented Interfaces
org.junit.jupiter.api.DisplayNameGenerator
Annotations
@API
status:STABLE
since:5.10

DisplayNameGenerator that generates complete sentences.

This generator generates display names that build up complete sentences by concatenating the names of the test and the enclosing classes. The sentence fragments are concatenated using a separator. The separator and the display name generator for individual sentence fragments can be configured via the @IndicativeSentencesGeneration annotation.

Since
5.7

Field Summary

Modifier and TypeField and Description
pack-priv static final DisplayNameGenerator

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static Optional<DisplayNameGeneration>

Returns:

an Optional containing the annotation, potentially empty if not found
findDisplayNameGeneration
(Class<?>
the test class on which to find the annotation; never null
testClass
)

Find the first DisplayNameGeneration annotation that is either directly present, meta-present, or indirectly present on the supplied testClass or on an enclosing class.

private static Optional<IndicativeSentencesGeneration>

Returns:

an Optional containing the annotation, potentially empty if not found
findIndicativeSentencesGeneration
(Class<?>
the test class on which to find the annotation; never null
testClass
)

Find the first IndicativeSentencesGeneration annotation that is either directly present, meta-present, or indirectly present on the supplied testClass or on an enclosing class.

public String
generateDisplayNameForClass(Class<?>
the class to generate a name for; never null
testClass
)

Implements org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForClass.

Generate a display name for the given top-level or static nested test class.
public String
generateDisplayNameForMethod(Class<?>
the class the test method is invoked on; never null
testClass
,
Method
method to generate a display name for; never null
testMethod
)

Implements org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForMethod.

Generate a display name for the given method.
public String
generateDisplayNameForNestedClass(Class<?>
the class to generate a name for; never null
nestedClass
)

Implements org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForNestedClass.

Generate a display name for the given @Nested inner test class.
private static String

Returns:

the sentence fragment separator
getFragmentSeparator
(Class<?>
the test class to search on for @IndicativeSentencesGeneration
testClass
)

Get the sentence fragment separator.

private static DisplayNameGenerator

Returns:

the DisplayNameGenerator instance to use
getGeneratorFor
(Class<?>
the test class to search on for @IndicativeSentencesGeneration
testClass
)

Get the display name generator to use for the supplied test class.

private String
private static Predicate<Class<?>>
not(Class<?> clazz)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

INSTANCEback to summary
pack-priv static final DisplayNameGenerator INSTANCE

Constructor Detail

IndicativeSentencesback to summary
public IndicativeSentences()

Method Detail

findDisplayNameGenerationback to summary
private static Optional<DisplayNameGeneration> findDisplayNameGeneration(Class<?> testClass)

Find the first DisplayNameGeneration annotation that is either directly present, meta-present, or indirectly present on the supplied testClass or on an enclosing class.

Parameters
testClass:Class<?>

the test class on which to find the annotation; never null

Returns:Optional<DisplayNameGeneration>

an Optional containing the annotation, potentially empty if not found

findIndicativeSentencesGenerationback to summary
private static Optional<IndicativeSentencesGeneration> findIndicativeSentencesGeneration(Class<?> testClass)

Find the first IndicativeSentencesGeneration annotation that is either directly present, meta-present, or indirectly present on the supplied testClass or on an enclosing class.

Parameters
testClass:Class<?>

the test class on which to find the annotation; never null

Returns:Optional<IndicativeSentencesGeneration>

an Optional containing the annotation, potentially empty if not found

generateDisplayNameForClassback to summary
public String generateDisplayNameForClass(Class<?> testClass)

Implements org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForClass.

Doc from org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForClass.

Generate a display name for the given top-level or static nested test class.

If it returns null, the default display name generator will be used instead.

Parameters
testClass:Class<?>

the class to generate a name for; never null

Returns:String

the display name for the class; never blank

Annotations
@Override
generateDisplayNameForMethodback to summary
public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod)

Implements org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForMethod.

Doc from org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForMethod.

Generate a display name for the given method.

If it returns null, the default display name generator will be used instead.

Parameters
testClass:Class<?>

the class the test method is invoked on; never null

testMethod:Method

method to generate a display name for; never null

Returns:String

the display name for the test; never blank

Annotations
@Override
generateDisplayNameForNestedClassback to summary
public String generateDisplayNameForNestedClass(Class<?> nestedClass)

Implements org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForNestedClass.

Doc from org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForNestedClass.

Generate a display name for the given @Nested inner test class.

If it returns null, the default display name generator will be used instead.

Parameters
nestedClass:Class<?>

the class to generate a name for; never null

Returns:String

the display name for the nested class; never blank

Annotations
@Override
getFragmentSeparatorback to summary
private static String getFragmentSeparator(Class<?> testClass)

Get the sentence fragment separator.

If @IndicativeSentencesGeneration is present (searching enclosing classes if not found locally), the configured separator will be used. Otherwise, IndicativeSentencesGeneration#DEFAULT_SEPARATOR will be used.

Parameters
testClass:Class<?>

the test class to search on for @IndicativeSentencesGeneration

Returns:String

the sentence fragment separator

getGeneratorForback to summary
private static DisplayNameGenerator getGeneratorFor(Class<?> testClass)

Get the display name generator to use for the supplied test class.

If @IndicativeSentencesGeneration is present (searching enclosing classes if not found locally), the configured generator will be used. Otherwise, IndicativeSentencesGeneration#DEFAULT_GENERATOR will be used.

Parameters
testClass:Class<?>

the test class to search on for @IndicativeSentencesGeneration

Returns:DisplayNameGenerator

the DisplayNameGenerator instance to use

getSentenceBeginningback to summary
private String getSentenceBeginning(Class<?> testClass)
notback to summary
private static Predicate<Class<?>> not(Class<?> clazz)
org.junit.jupiter.api back to summary

public Class DisplayNameGenerator.ReplaceUnderscores

extends Simple
Class Inheritance

DisplayNameGenerator that replaces underscores with spaces.

This generator extends the functionality of Simple by replacing all underscores ('_') found in class and method names with spaces (' ').

Field Summary

Modifier and TypeField and Description
pack-priv static final DisplayNameGenerator

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public String
generateDisplayNameForClass(Class<?>
the class to generate a name for; never null
testClass
)

Overrides org.junit.jupiter.api.DisplayNameGenerator.Standard.generateDisplayNameForClass.

Implements org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForClass.

Generate a display name for the given top-level or static nested test class.
public String
generateDisplayNameForMethod(Class<?>
the class the test method is invoked on; never null
testClass
,
Method
method to generate a display name for; never null
testMethod
)

Overrides org.junit.jupiter.api.DisplayNameGenerator.Simple.generateDisplayNameForMethod.

Implements org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForMethod.

Generate a display name for the given method.
public String
generateDisplayNameForNestedClass(Class<?>
the class to generate a name for; never null
nestedClass
)

Overrides org.junit.jupiter.api.DisplayNameGenerator.Standard.generateDisplayNameForNestedClass.

Implements org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForNestedClass.

Generate a display name for the given @Nested inner test class.
private static String

Field Detail

INSTANCEback to summary
pack-priv static final DisplayNameGenerator INSTANCE

Hides org.junit.jupiter.api.DisplayNameGenerator.Simple.INSTANCE.

Constructor Detail

ReplaceUnderscoresback to summary
public ReplaceUnderscores()

Method Detail

generateDisplayNameForClassback to summary
public String generateDisplayNameForClass(Class<?> testClass)

Overrides org.junit.jupiter.api.DisplayNameGenerator.Standard.generateDisplayNameForClass.

Implements org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForClass.

Doc from org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForClass.

Generate a display name for the given top-level or static nested test class.

If it returns null, the default display name generator will be used instead.

Parameters
testClass:Class<?>

the class to generate a name for; never null

Returns:String

the display name for the class; never blank

Annotations
@Override
generateDisplayNameForMethodback to summary
public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod)

Overrides org.junit.jupiter.api.DisplayNameGenerator.Simple.generateDisplayNameForMethod.

Implements org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForMethod.

Doc from org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForMethod.

Generate a display name for the given method.

If it returns null, the default display name generator will be used instead.

Parameters
testClass:Class<?>

the class the test method is invoked on; never null

testMethod:Method

method to generate a display name for; never null

Returns:String

the display name for the test; never blank

Annotations
@Override
generateDisplayNameForNestedClassback to summary
public String generateDisplayNameForNestedClass(Class<?> nestedClass)

Overrides org.junit.jupiter.api.DisplayNameGenerator.Standard.generateDisplayNameForNestedClass.

Implements org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForNestedClass.

Doc from org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForNestedClass.

Generate a display name for the given @Nested inner test class.

If it returns null, the default display name generator will be used instead.

Parameters
nestedClass:Class<?>

the class to generate a name for; never null

Returns:String

the display name for the nested class; never blank

Annotations
@Override
replaceUnderscoresback to summary
private static String replaceUnderscores(String name)
org.junit.jupiter.api back to summary

public Class DisplayNameGenerator.Simple

extends Standard
Class Inheritance
Known Direct Subclasses
org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores

Simple DisplayNameGenerator that removes trailing parentheses for methods with no parameters.

This generator extends the functionality of Standard by removing parentheses ('()') found at the end of method names with no parameters.

Field Summary

Modifier and TypeField and Description
pack-priv static final DisplayNameGenerator

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public String
generateDisplayNameForMethod(Class<?>
the class the test method is invoked on; never null
testClass
,
Method
method to generate a display name for; never null
testMethod
)

Overrides org.junit.jupiter.api.DisplayNameGenerator.Standard.generateDisplayNameForMethod.

Implements org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForMethod.

Generate a display name for the given method.
private static boolean
Inherited from org.junit.jupiter.api.DisplayNameGenerator.Standard:
generateDisplayNameForClassgenerateDisplayNameForNestedClass

Field Detail

INSTANCEback to summary
pack-priv static final DisplayNameGenerator INSTANCE

Hides org.junit.jupiter.api.DisplayNameGenerator.Standard.INSTANCE.

Constructor Detail

Simpleback to summary
public Simple()

Method Detail

generateDisplayNameForMethodback to summary
public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod)

Overrides org.junit.jupiter.api.DisplayNameGenerator.Standard.generateDisplayNameForMethod.

Implements org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForMethod.

Doc from org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForMethod.

Generate a display name for the given method.

If it returns null, the default display name generator will be used instead.

Parameters
testClass:Class<?>

the class the test method is invoked on; never null

testMethod:Method

method to generate a display name for; never null

Returns:String

the display name for the test; never blank

Annotations
@Override
hasParametersback to summary
private static boolean hasParameters(Method method)
org.junit.jupiter.api back to summary

public Class DisplayNameGenerator.Standard

extends Object
implements DisplayNameGenerator
Class Inheritance
All Implemented Interfaces
org.junit.jupiter.api.DisplayNameGenerator
Known Direct Subclasses
org.junit.jupiter.api.DisplayNameGenerator.Simple

Standard DisplayNameGenerator.

This implementation matches the standard display name generation behavior in place since JUnit Jupiter 5.0 was released.

Field Summary

Modifier and TypeField and Description
pack-priv static final DisplayNameGenerator

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public String
generateDisplayNameForClass(Class<?>
the class to generate a name for; never null
testClass
)

Implements org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForClass.

Generate a display name for the given top-level or static nested test class.
public String
generateDisplayNameForMethod(Class<?>
the class the test method is invoked on; never null
testClass
,
Method
method to generate a display name for; never null
testMethod
)

Implements org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForMethod.

Generate a display name for the given method.
public String
generateDisplayNameForNestedClass(Class<?>
the class to generate a name for; never null
nestedClass
)

Implements org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForNestedClass.

Generate a display name for the given @Nested inner test class.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

INSTANCEback to summary
pack-priv static final DisplayNameGenerator INSTANCE

Constructor Detail

Standardback to summary
public Standard()

Method Detail

generateDisplayNameForClassback to summary
public String generateDisplayNameForClass(Class<?> testClass)

Implements org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForClass.

Doc from org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForClass.

Generate a display name for the given top-level or static nested test class.

If it returns null, the default display name generator will be used instead.

Parameters
testClass:Class<?>

the class to generate a name for; never null

Returns:String

the display name for the class; never blank

Annotations
@Override
generateDisplayNameForMethodback to summary
public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod)

Implements org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForMethod.

Doc from org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForMethod.

Generate a display name for the given method.

If it returns null, the default display name generator will be used instead.

Parameters
testClass:Class<?>

the class the test method is invoked on; never null

testMethod:Method

method to generate a display name for; never null

Returns:String

the display name for the test; never blank

Annotations
@Override
generateDisplayNameForNestedClassback to summary
public String generateDisplayNameForNestedClass(Class<?> nestedClass)

Implements org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForNestedClass.

Doc from org.junit.jupiter.api.DisplayNameGenerator.generateDisplayNameForNestedClass.

Generate a display name for the given @Nested inner test class.

If it returns null, the default display name generator will be used instead.

Parameters
nestedClass:Class<?>

the class to generate a name for; never null

Returns:String

the display name for the nested class; never blank

Annotations
@Override