Annotations

Learn how to write some basic tests and annotations in JUnit 5.

The @Test annotation

JUnit 5 uses different annotations to annotate test methods. The org.junit.jupiter.api.Test annotates a method as a test method executed by JUnit. The @Test annotation has no attributes.

package io.educative.junit5;
import org.junit.jupiter.api.Test;
class BasicTest {
@Test
void emptyTest() {
}
}

The @DisplayName annotation

The org.junit.jupiter.api.DisplayName annotation adds custom display names to test classes and test methods. The @DisplayName annotation resolves the longtime debate about how to name test methods. Some developers want to use the standard camel casesIn this case, the first letter of each word is capitalized., like testAddTwoNumbers, while others prefer the snake caseIn this case, we will replace spaces between words with underscores., like test_add_two_numbers. We’re now free to name test methods and use @DisplayName to add the names displayed in test runners and reports.

import org.junit.jupiter.api.DisplayName;
public class EmptyTest {
@Test
@DisplayName("An empty test")
public void emptyTest() {
}
}