Hey guys in this post, we will discuss some of the best practices of @DisplayName
annotation in JUnit.
Read More:
- Check the Complete JUnit 5 Tutorial
- Check the Complete JavaServer Faces (JSF) Tutorial
- Check the Spring Boot JdbcTemplate Tutorials
- Check the Complete Spring Boot and Data JPA Tutorials
- Check the Complete Spring MVC Tutorials
- Check the Complete JSP Tutorials
- Check the Complete Spring Boot Tutorials [100+ Examples]
- Check the Complete Spring Boot and Thymeleaf Tutorial
- Check the Complete AWS Tutorial
- Check the Complete JavaServer Faces (JSF) Tutorial
- Check the Complete Spring Data JPA Tutorial
- Check the Complete Spring Security Tutorial
- Check the Javascript Projects for Beginners
About @DisplayName annotation
- JUnit 5 allows us to use custom names for test classes.
- We can use
org.junit.jupiter.api.DisplayName
annotation to provide a name that can contain spaces, special characters, or even emojis. - This allows us to use more meaningful names for test classes.
package in.bushansirgur;
import static org.junit.jupiter.api.Assertions.*;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("Movies")
class MoviesSpec {
@Test
@DisplayName("is empty when no movies are added")
void testMovies() {
Movies m = new Movies();
List list = m.getMovies();
assertTrue(list.isEmpty(), () -> "Movies should be empty");
}
}
We used @DisplayName
annotation with both the test class and the test method to provide readable names.
That’s it for this post, if you like this post, share this with your friends and colleagues or you can share this within your social media platform. Thanks, I will see you in our next post.
Every one is sharing information, that’s truly good, keep u? writing.