In this article we will see Junit 5 display names, how to set description for tests using @DisplayName, how to generate default names for tests using @DisplayNameGeneration and creating custom display name extending DisplayNameGenerator with examples.
1. @DisplayName
In Junit 5, test classes and test methods can declare custom display names via Junit 5 @DisplayName
annotation.
We can display names with spaces, special characters, and even emojis. That names will be displayed in test reports and by test runners and IDEs.
Let’s go through an example to see how the Junit 5 @DispalyName works.
2. Display Names example
2.1. Technologies used :
- Junit 5.5.2
- Maven 3
- Java 8
- Spring Tool Suite 3.9.8
2.2. Junit5DiplayNameTest.java
@DisplayName("MathUtil Test Case") public class Junit5DiplayNameTest { @Test @DisplayName("MathUtil add test") void test_Add() { assertEquals(5, MathUtil.add(3, 2)); } @Test @DisplayName("MathUtil multiply test") void test_Multiply() { assertEquals(15, MathUtil.multiple(3, 5)); } @Test @DisplayName("MathUtil devide test") void test_Devide() { assertEquals(5, MathUtil.devide(25, 5)); } @Test @DisplayName("MathUtil isPrime test") void test_IsPrime() { assertTrue(MathUtil.isPrime(13)); } }
2.3. Test Results
3. Display Name Generators
3.1. JUnit Jupiter supports custom display name generators that can be configured via the @DisplayNameGeneration
annotation.
3.2. Values provided via @DisplayName
annotations always take precedence over display names generated by a DisplayNameGenerator
.
3.3. This example uses the JUnit ReplaceUnderscores
generator to replace the underscores with spaces in the test name, it is in-built display name generator in Junit 5.
3.4. Junit5_Diplay_Name_Generator_Test.java
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) public class Junit5_Diplay_Name_Generator_Test { @Test void test_Add() { assertEquals(5, MathUtil.add(3, 2)); } @Test void test_Multiply() { assertEquals(15, MathUtil.multiple(3, 5)); } @Test void test_Devide() { assertEquals(5, MathUtil.devide(25, 5)); } @Test void test_IsPrime() { assertTrue(MathUtil.isPrime(13)); } }
3.5. Test Results
4. Custom Display Name Generators
4.1. We can extend the JUnit DisplayNameGenerator
to create our custom display name generator.
@DisplayNameGeneration(MathUtilTest.MyDisplayNameGenerator.class) public class MathUtilTest { @Test void test_Add() { assertEquals(5, MathUtil.add(3, 2)); } @Test void test_Multiply() { assertEquals(15, MathUtil.multiple(3, 5)); } @Test void test_Devide() { assertEquals(5, MathUtil.devide(25, 5)); } @Test void test_IsPrime() { assertTrue(MathUtil.isPrime(13)); } static class MyDisplayNameGenerator extends DisplayNameGenerator.Standard { @Override public String generateDisplayNameForClass(Class<?> testClass) { return super.generateDisplayNameForClass(testClass); } @Override public String generateDisplayNameForNestedClass(Class<?> nestedClass) { return super.generateDisplayNameForNestedClass(nestedClass) + "..."; } @Override public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod) { String name = testMethod.getName().replace('_', ' ').replace("test", ""); return name+" "+testClass.getSimpleName(); } } }
4.2.Test Results
5. Set default or global Display Name Generator
5.1. We can also set default DisplayNameGenerator
that can be used by default if we do not use @DisplayName annotation explicitly to the each Junit 5 test case.
5.2. We need to set junit.jupiter.displayname.generator.default
configuration parameter to specify the fully qualified class name of the DisplayNameGenerator
you would like to use by default.you can specify the fully qualified name of any custom class that implements DisplayNameGenerator
.
junit-platform.properties in src/test/resources
:
#junit.jupiter.displayname.generator.default = org.junit.jupiter.api.DisplayNameGenerator$ReplaceUnderscores junit.jupiter.displayname.generator.default = com.javabydeveloper.util.MathUtilTest$MyDisplayNameGenerator
In summary, the display name for a test class or method is determined according to the following precedence rules:
- value of the
@DisplayName
annotation, if present - by calling the
DisplayNameGenerator
specified in the@DisplayNameGeneration
annotation, if present - by calling the default
DisplayNameGenerator
configured via the configuration parameter, if present - by calling
org.junit.jupiter.api.DisplayNameGenerator.Standard
You also might be interested in following examples :
- Junit 5 Dynamic Tests
- Junit 5 tags and filter tests
- Junit 5 Timeout tests
- Junit 5 ConsoleLauncher examples
- Junit 5 maven example
- Junit 5 Disable tests
- Junit 5 nested tests
- Junit 5 parallel execution