DisplayNameGeneration
Using the DisplayNameGeneration annotation we can define the strategy to be used for the generation of the test names. We can only use the DisplayNameGeneration for classes. Let us look at the DisplayNameGenerator.ReplaceUnderscores.class,
@Nested
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class underscoreReplace {
@Test
void shouldGenerate() {}
@DisplayName("@DisplayName annotation is preferred")
@Test
void notGenerate() {}
}
In this code snippet, we demonstrated that the DisplayName annotation always takes priority over the generated names. We created two functions named shouldGenerate and notGenerate. We observe that the shouldGenerate function is called.
IndicativeSentenceGeneration
The IndicativeSentenceGeneration annotation can be used to generate names by combining the enclosing test class names and the method names.
@Nested
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class replaceUnderscores {
@Test
void ifNotBlank() {}
@DisplayName("@DisplayName - if only numbers or alphabets")
@ParameterizedTest(name = "{displayName} -> {0} valid string")
@ValueSource(strings = {"b", "2", "b2"})
void onlyNumOrAlpha(String value) {}
}
In the above code, we created two functions, namely, ifNotBlank and onlyNumOrAlpha.
ParameterizedTest
Using the ParameterizedTest annotation we can change the name of the test class or the method by defining a field name, for instance.
the enclosing test class names and the method names.
@Nested
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class replaceUnderscores {
@Test
void ifNotBlank() {}
@DisplayName("@DisplayName - if special characters")
@ParameterizedTest(name = "{displayName} -> {0} not valid string")
@ValueSource(strings = {"#", "b#", "b#@"})
void onlyNumOrAlpha(String value) {}
}
FAQs
-
What is JUnit?
JUnit is a framework in Java used for unit testing. It is vital in the development of test-driven development. It is part of the xUnit frameworks. We link the JUnit as a JAR file at compile time. JUnit5 resides under the org.junit.jupiter package in Java.
-
What is the latest version of JUnit?
The most recent version of JUnit is Junit5, although the previous versions of Junit are still equally famous. For instance, JUnit4 has over 100,000 usages.
-
What is Dynamic Testing?
A Dynamic Test is one of the most commonly used tests, and it is generated at the run time. We use the TestFactory method to generate Dynamic Tests in JUnit5. TestFactory method is a non-static and non-private method.
-
What is the DisplayName annotation?
We can use the DisplayName annotation to provide a custom name to our test classes or methods.
-
What is the benefit of using DisplayName?
The benefit of using DisplayName annotation is that it allows the developer to add special characters and emojis to the name of the test class or the method.
-
What is camel casing?
Camel casing is the practice of writing words without spaces or punctuations. The separation of words is indicated using capital letters. The first letter starts with either case. For example, onlyNumOrAlpha, ifNumOrAlpha.
Key Takeaways
This Blog covered all the necessary points about the Display Name Generator and all the features of the Display Name Generator in JUnit. We further discussed how to implement various types of Name Generators in JUnit.
Don’t stop here; check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems.