Junit 5 Example : Display Name

September 30, 2020 | No comments

JUnit 5 Tutorial : Display name

In JUnit 5, we can use @DisplayName to declare custom display names for test classes and test methods.

Display Name Example

package com.bootng.display;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public class DisplayCustomNameTest {

	@Test
	public void test_if_it_will_rain() {
	}

	@Test
	public void test_if_it_will_be_cloudy() {
	}

	@Test
	public void test_if_it_will_be_sunny() {
	}

}

Output




Display CustomName Example

package com.bootng.display;
@DisplayName("Vacation Weather Test")
public class DisplayCustNameTest {
	@DisplayName("🌧")
	@Test
	public void test_if_it_will_rain() {
	}

	@DisplayName("🌨")
	@Test
	public void test_if_it_will_be_cloudy() {
	}

	@DisplayName("🌞")
	@Test
	public void test_if_it_will_be_sunny() {
	}
}
Output

Custom Name Generator Example

We can also use a Custom Name generator to generate the test names. As of Version 5.4 it comes with the following generators out of the box.

DisplayNameGenerator.IndicativeSentences
DisplayNameGenerator.ReplaceUnderscores
DisplayNameGenerator.Simple
DisplayNameGenerator.Standard

Each of the above generators has different implementations of how to generate test name. 

In the Following example we are using ReplaceUnderscores.  As a result the test names will be generated by replacing the "_" from respective names.
package com.bootng.display;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("Weather Test DisplayExample ")
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
public class DisplayGeneratorExampleTest {

	@Test
	public void test_if_it_will_rain() {
	}

	@Test
	public void test_if_it_will_be_cloudy() {
	}

	@Test
	public void test_if_it_will_be_sunny() {
	}
}

Output


No comments :

Post a Comment

Please leave your message queries or suggetions.