JUnit test description

java, junit, testing

Solution

In JUnit 5, there is @DisplayName annotation:

@DisplayName is used to declare a custom display name for the annotated test class or test method. Display names are typically used for test reporting in IDEs and build tools and may contain spaces, special characters, and even emoji.

Example:

@Test
@DisplayName("Test if true holds")
public void checkTrue() {
    assertEquals(true, true);
}

Problem

Is it possible in JUnit to add a brief description of the test for the future reader (e.g. what's being tested, some short explanation, expected result, ...)? I mean something like in ScalaTest, where I can write: ``` test("Testing if true holds") { assert(true) } ``` Ideal approach would be using some annotation, e.g. ``` @Test @TestDescription("Testing if true holds") public void testTrue() { assert(true); } ``` Therefore, if I run such annotated tests using Maven (or some similar tool), I could have similar output to the one I have in SBT when using ScalaTest: ``` - Testing if entity gets saved correctly - Testing if saving fails when field Name is not specified - ... ``` Currently I can either use terribly long method names or write javadoc comments, which are not present in the build output. Thank you.

Original source