PHpullh

JAVA · 테스트

JUnit 5 심화

JUnit 5의 생명주기, 표시 이름, 조건부 실행을 다룹니다.

테스트중급JUnit5Jupiter테스트ParameterizedTest

핵심 설명

JUnit 5의 생명주기, 표시 이름, 조건부 실행을 다룹니다.

Java code

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

// JUnit 5 사용법 (의사코드 — 테스트 실행 시 의존성 필요)
class CalculatorTest {
    // Calculator calc;

    // @BeforeAll  // 클래스당 1번 (static)
    // static void setupAll() { System.out.println("전체 초기화"); }

    // @BeforeEach // 각 테스트 전
    // void setup() { calc = new Calculator(); }

    // @Test
    // @DisplayName("덧셈: 양수 + 양수")
    // void addPositiveNumbers() {
    //     assertEquals(5, calc.add(2, 3));
    // }

    // @Test
    // @Disabled("버그 수정 후 활성화")
    // void brokenTest() { fail("아직 구현 안 됨"); }

    // @RepeatedTest(5)
    // @DisplayName("랜덤 테스트")
    // void repeatedTest(RepetitionInfo info) {
    //     System.out.println("반복: " + info.getCurrentRepetition());
    // }

    // @ParameterizedTest
    // @ValueSource(ints = {1, 2, 3, 4, 5})
    // void isPositive(int number) {
    //     assertTrue(number > 0);
    // }

    // @AfterEach  // 각 테스트 후
    // void tearDown() { calc = null; }

    // @AfterAll   // 클래스당 1번 (static)
    // static void tearDownAll() { System.out.println("전체 정리"); }

    public static void main(String[] args) {
        System.out.println("JUnit 5 = Jupiter API + Platform + Vintage(JUnit 4 호환)");
    }
}

학습 팁

@ParameterizedTest로 같은 로직을 여러 입력값에 대해 테스트하면 코드 중복을 줄이고 커버리지를 높입니다.

주의할 점

@BeforeAll@AfterAll 메서드는 기본적으로 static이어야 합니다. @TestInstance(Lifecycle.PER_CLASS)를 사용하면 비정적으로 만들 수 있습니다.

자주 묻는 질문

JUnit 5 심화란 무엇인가요?

JUnit 5의 생명주기, 표시 이름, 조건부 실행을 다룹니다.

JUnit 5 심화 학습 시 주의할 점은 무엇인가요?

@BeforeAll 과 @AfterAll 메서드는 기본적으로 static 이어야 합니다. @TestInstance(Lifecycle.PER_CLASS) 를 사용하면 비정적으로 만들 수 있습니다.