<aside> 💡 [참고]

테스트 코드 시작 방법

</aside>

<aside> 💡 [참고]

2.2+ 버전의 스프링 부트 프로젝트를 만든다면 기본으로 JUnit 5 의존성 추가됩니다.

</aside>

JUnit 5 테스트 코드

package hello.javatest;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class StudyTest { //...1

    @Test
    void create() { //...1
        Study study = new Study();
        assertNotNull(study);
    }
}
  1. JUnit 5 부터는 클래스나 메소드가 public 이 아니여도 상관 없습니다.

    JUnit 4 에서는 둘 다 반드시 public 클래스 안에 public 메소드만 실행가능했지만 JUnit 5 에서는 이런 제약이 사라졌습니다.

    자바 리플렉션을 사용하게 되면 private 하거나 default 한 메소드도 접근하고 실행 가능하니까 굳이 public이 필요 없는 것입니다.

기본 애노테이션

package hello.javatest;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class StudyTest {

    @Test //...1
    void create() {
        Study study = new Study();
        assertNotNull(study);
        System.out.println("create");
    }

    @Test
    @Disabled //...6
    void create1() {
        System.out.println("create1");
    }

    @BeforeAll //...2
    static void beforeAll() {
        System.out.println("before all");
    }

    @AfterAll //...3
    static void afterAll() {
        System.out.println("after all");
    }

    @BeforeEach //...4
    void beforeEach() {
        System.out.println("before each");
    }

    @AfterEach //...5
    void AfterEach() {
        System.out.println("After each");
    }
}
  1. @Test
  2. @BeforeAll
  3. @AfterAll
  4. @BeforeEach
  5. @AfterEach
  6. @Disabled