In JUnit 5, Test classes and test methods may declare custom display names using @DisplayName — with spaces, special characters, and even emojis — that will be displayed in test reports and by test runners and IDEs.
1.The default name of test classes and methods
The default name of test class and test methods are the same as a class or test method name:
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>DefaultNameOfTestClassesAndMethodsExample</groupId> <artifactId>DefaultNameOfTestClassesAndMethodsExample</artifactId> <version>0.0.1-SNAPSHOT</version> <name>com.kkjavatutorilas</name> <properties> <maven.compiler.target>8</maven.compiler.target> <maven.compiler.source>8</maven.compiler.source> <junit.jupiter.version>5.6.2</junit.jupiter.version> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> </dependencies> </project> |
Java Class for that we have to write JUnit Tests:
1 2 3 4 5 6 7 8 |
package com.kkjavatutorials; public class MyUtils { public int add(int n1, int n2) { return n1+n2; } } |
Test class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package com.kkjavatutorials; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public class MyUtilsTest { private static MyUtils myUtils; @BeforeAll static void setUp() { myUtils = new MyUtils(); System.out.println("Test data set up is done.."); } @BeforeEach void beforeEach() { System.out.println("@BeforeEach is executed.."); } @Test void test_add_two_positive_numbers() { int actualResult = myUtils.add(20, 10); assertEquals(30, actualResult); } @Test void test_add_two_negative_numbers() { int actualResult = myUtils.add(-20, -10); assertEquals(-30, actualResult); } @Test void test_add_one_positive_and_one_negative_number() { int actualResult = myUtils.add(20, -10); assertEquals(10, actualResult); } @AfterEach void afterEach() { System.out.println("@AfterEach is executed.."); } @AfterAll static void tearDown() { myUtils = null; System.out.println("Test data teardown is done.."); } } |
The output of the above project:
2. Customize the name of test class and test methods using @DisplayName Annotation
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkjavatutorials</groupId> <artifactId>JUnit5DisplayNameExample</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <maven.compiler.target>8</maven.compiler.target> <maven.compiler.source>8</maven.compiler.source> <junit.jupiter.version>5.6.2</junit.jupiter.version> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> </dependencies> </project> |
Java Class for that we have to write JUnit Tests:
1 2 3 4 5 6 7 8 |
package com.kkjavatutorials; public class MyUtils { public int add(int n1, int n2) { return n1+n2; } } |
Test class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
package com.kkjavatutorials; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @DisplayName(value = "All the Test Cases of MyUtils") public class MyUtilsTest { private static MyUtils myUtils; @BeforeAll static void setUp() { myUtils = new MyUtils(); System.out.println("Test data set up is done.."); } @BeforeEach void beforeEach() { System.out.println("@BeforeEach is executed.."); } @Test @DisplayName(value = "adding two positive numbers") void test_add_two_positive_numbers() { int actualResult = myUtils.add(20, 10); assertEquals(30, actualResult); } @Test @DisplayName(value = "adding two negtaive numbers") void test_add_two_negative_numbers() { int actualResult = myUtils.add(-20, -10); assertEquals(-30, actualResult); } @Test @DisplayName(value = "😱") void test_add_one_positive_and_one_negative_number() { int actualResult = myUtils.add(20, -10); assertEquals(10, actualResult); } @AfterEach void afterEach() { System.out.println("@AfterEach is executed.."); } @AfterAll static void tearDown() { myUtils = null; System.out.println("Test data teardown is done.."); } } |
The output of the above project:
You can see the output of the above test display names is customized.
3. Display Name Generators
In JUnit Jupiter, we can create custom display name generators that can be configured via the @DisplayNameGeneration annotation. One thing you should note here that values specified via @DisplayName annotations always take precedence over display names generated by a DisplayNameGenerator
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkjavatutorials</groupId> <artifactId>JUnit5DisplayNameGeneratorExample</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <maven.compiler.target>8</maven.compiler.target> <maven.compiler.source>8</maven.compiler.source> <junit.jupiter.version>5.6.2</junit.jupiter.version> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> </dependencies> </project> |
Java Class for that we have to write JUnit Tests:
1 2 3 4 5 6 7 8 |
package com.kkjavatutorials; public class MyUtils { public int add(int n1, int n2) { return n1+n2; } } |
Test Classes:
MyUtilsTest1.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
package com.kkjavatutorials; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; import org.junit.jupiter.api.Test; @DisplayNameGeneration(value = DisplayNameGenerator.ReplaceUnderscores.class) public class MyUtilsTest1 { private static MyUtils myUtils; @BeforeAll static void setUp() { myUtils = new MyUtils(); System.out.println("Test data set up is done.."); } @BeforeEach void beforeEach() { System.out.println("@BeforeEach is executed.."); } @Test void test_add_two_positive_numbers() { int actualResult = myUtils.add(20, 10); assertEquals(30, actualResult); } @Test void test_add_two_negative_numbers() { int actualResult = myUtils.add(-20, -10); assertEquals(-30, actualResult); } @Test void test_add_one_positive_and_one_negative_number() { int actualResult = myUtils.add(20, -10); assertEquals(10, actualResult); } @AfterEach void afterEach() { System.out.println("@AfterEach is executed.."); } @AfterAll static void tearDown() { myUtils = null; System.out.println("Test data teardown is done.."); } } |
MyUtilsTest2.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
package com.kkjavatutorials; import static org.junit.jupiter.api.Assertions.assertEquals; import java.lang.reflect.Method; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; import org.junit.jupiter.api.Test; @DisplayNameGeneration(value = MyUtilsTest2.CustomDiplayNameGenerator.class) public class MyUtilsTest2 { private static MyUtils myUtils; @BeforeAll static void setUp() { myUtils = new MyUtils(); System.out.println("Test data set up is done.."); } @BeforeEach void beforeEach() { System.out.println("@BeforeEach is executed.."); } @Test void test_add_two_positive_numbers() { int actualResult = myUtils.add(20, 10); assertEquals(30, actualResult); } @Test void test_add_two_negative_numbers() { int actualResult = myUtils.add(-20, -10); assertEquals(-30, actualResult); } @Test void test_add_one_positive_and_one_negative_number() { int actualResult = myUtils.add(20, -10); assertEquals(10, actualResult); } @AfterEach void afterEach() { System.out.println("@AfterEach is executed.."); } @AfterAll static void tearDown() { myUtils = null; System.out.println("Test data teardown is done.."); } static class CustomDiplayNameGenerator extends DisplayNameGenerator.ReplaceUnderscores{ @Override public String generateDisplayNameForClass(Class<?> testClass) { return testClass.getName()+" Test Cases"; } @Override public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod) { String name = testClass.getSimpleName()+" "+testMethod.getName(); return name.replace('_', ' '); } @Override public String generateDisplayNameForNestedClass(Class<?> nestedClass) { return super.generateDisplayNameForNestedClass(nestedClass); } } } |
The output of the above project:
4. Parameterized Tests Customization display Name:
We can also customize the display name of Parameterized Tests via the name attribute of the @ParameterizedTest, see the following example
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkjavatutorials</groupId> <artifactId>JUnit5CustomizingDisplayNamesInParameterizedTestsExample</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <maven.compiler.target>8</maven.compiler.target> <maven.compiler.source>8</maven.compiler.source> <junit.jupiter.version>5.6.2</junit.jupiter.version> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-params</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> </dependencies> </project> |
Test Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.kkjavatutorials; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; public class CustomizingDisplayNamesInParameterizedTest { @DisplayName("Display name of fruit container") @ParameterizedTest(name = "{index} ==> the rank of ''{0}'' is {1}") //@ParameterizedTest(name = "{arguments}") @CsvSource({ "apple, 1", "banana, 2", "'lemon, lime', 3" }) void testWithCustomDisplayNames(String fruitName, int fruitRank) { assertTrue(!fruitName.isEmpty()); assertTrue(fruitRank > 0); } } |
The output of the above project:
You May Also Like:
Junit 5 Architecture
JUnit 5 Annotations
JUnit 5 Maven Dependency
JUnit 5 with Gradle Dependency
JUnit 5 Test Lifecycle
JUnit 5 @BeforeAll annotation example
Unit 5 @AfterAll annotation example
JUnit 5 @BeforeEach and @AfterEach annotation Example
That’s all about JUnit 5 Display Names
If you have any feedback or suggestion please feel free to drop in below comment box.