JUnit Display Name Best Practices

Hey guys in this post, we will discuss some of the best practices of @DisplayName annotation in JUnit.




Read More:

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.

Screenshot-2022-07-10-at-8-06-59-PM

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.




About the author

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

View all posts

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *