JUnit 5 Example : Custom Display Name
In JUnit 5, we can use @DisplayName to declare custom display names for test classes and test methods.
The custom display name will be visible while debugging or while generating any reports from test execution.
First Let's see an example of Test Class without using Display Name.
Example 1 without Display name
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() {
}
}
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.
Custom Name Generator Example
DisplayNameGenerator.IndicativeSentences
DisplayNameGenerator.ReplaceUnderscores
DisplayNameGenerator.Simple
DisplayNameGenerator.Standard
Each of the above generators have different implementations of how to generate test name. In this article we will see how to use the ReplaceUnderscores.
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() {
}
}
No comments :
Post a Comment
Please leave your message queries or suggetions.
Note: Only a member of this blog may post a comment.