Annotations
Learn how to write some basic tests and annotations in JUnit 5.
We'll cover the following
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 {@Testvoid 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 testAddTwoNumbers
, while others prefer the 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() {}}