JUnit 5 Basic Tests - @Test, @DisplayName, @Disabled

Explanation of JUnit 5 basic tests with @Test, @DisplayName, and @Disabled

Failed and Successful Tests

The following example creates simple test cases that succeed and fail.

package com.devkuma.junit5.basic;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class SuccessFailTest {

    @Test
    void success() {
        Assertions.assertEquals(3, 3);
    }

    @Test
    void fail() {
        Assertions.assertEquals(3, 2);
    }
}

Execution result:

O success()
X fail()
  • A method annotated with @Test is a test method.
  • Up to JUnit 4, org.junit.Test was used, but JUnit 5 uses org.junit.jupiter.api.Test.

Displaying Test Names with @DisplayName

With JUnit 5 @DisplayName, you do not need to write method names in Korean like this:

@Test
public void 게시글저장() {
     // ... omitted ...
}
...

You can write them as follows instead.

@Test
@DisplayName("게시글 저장")
public void savePost() {
     // ... omitted ...
}

Disabling Tests

Using the @Disabled annotation prevents a test method from being executed.

package com.devkuma.junit5.basic;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

public class DisabledTest {
    @Test
    void test1() {
        System.out.println("test1()");
    }

    @Test
    @Disabled
    void test2() {
        System.out.println("test2()");
    }
}

Execution result:

test1()

@Disabled can be specified not only on methods but also on classes. In that case, all test methods in the test class are not executed.