Home Junit 5 Junit 5 Dispaly names for tests

Junit 5 Dispaly names for tests

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 :

  1. Junit 5.5.2
  2. Maven 3
  3. Java 8
  4. Spring Tool Suite 3.9.8

Junit 5 @DisplayName example

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

Junit 5 @DisplayName test results 1

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

Junit-5-@DisplayNameGeneration-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

Junit-5-custom-display-name-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:

  1. value of the @DisplayName annotation, if present
  2. by calling the DisplayNameGenerator specified in the @DisplayNameGeneration annotation, if present
  3. by calling the default DisplayNameGenerator configured via the configuration parameter, if present
  4. by calling org.junit.jupiter.api.DisplayNameGenerator.Standard

You also might be interested in following examples :

  1. Junit 5 Dynamic Tests and @TestFactory annotation.
  2. Junit 5 tags and filter test cases for execution.
  3. Junit 5 Timeout tests, fail tests if not completed within time.
  4. Junit 5 ConsoleLauncher examples
  5. Junit 5 maven example
  6. Junit 5 Disable tests using @Disabled annotation

Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories