Java & Spring/Spring

46일차 - 테스트 코드 애노테이션

DJ.Kang 2024. 9. 10. 15:39

□ Before & After 테스트

  1. @BeforeEach : 각각의 테스트 코드가 실행되기 전
  2. @AfterEach : 각각의 테스트 코드가 실행된 후
  3. @BeforeAll : 모든 테스트 코드가 실행되기 전
  4. @AfterAll : 모든 테스트 코드가 실행된 후


□ Custom 테스트

1. @DisplayName("") : 테스트에 표시될 이름 설정

2. @Nested : 테스트를 그룹지을 때 사용

 

3. @TestMethodOrder(MethodOrderer.OrederAnnotation.class) : 테스트의 순서를 지정


□ Repeat 테스트

  1. @RepeatedTest(value = n, name = "") : value값만큼 반복
  2. @ParameterizedTest : 매개변수 값을 변경해가며 반복
    @ValueSource(ints = {1, 2, 3, 4...})
public class RepeatTest {
    @RepeatedTest(value = 5, name = "반복 테스트 {currentRepetition} / {totalRepetitions}")
    void repeatTest(RepetitionInfo info) {
        System.out.println("테스트 반복 : 
        " + info.getCurrentRepetition() + " / " + info.getTotalRepetitions());
    }

    @DisplayName("파라미터 값 활용하여 테스트 하기")
    @ParameterizedTest
    @ValueSource(ints = {1, 2, 3, 4, 5, 6, 7, 8, 9})
    void parameterTest(int num) {
        System.out.println("5 * num = " + 5 * num);
    }
}

 


□ Assertions 테스트

1. assertEquals(expect, result) &  asswerNotEquals(expect, result) : (기대 값)과 (결과 값) 비교 [같을 시 성공 & 다를 시 성공]

    @Test
    void test1() {
        Double result = calculator.operate(5, "/", 2);
        assertEquals(2.5, result);
    }
    
    @Test
    void test1_2() {
        Double result = calculator.operate(5, "/", 0);
        assertNotEquals(2.5, result);
    }

 

2. assertEquals(expect, result, new Supplier<String>()) : 테스트 실패 시 메세치 출력

    @Test
    void test1_1() {
        Double result = calculator.operate(5, "/", 0);
        // 테스트 실패 시 메시지 출력 (new Supplier<String>())
        assertEqu
    }

 

3. assertTrue & asserFalse : Boolean값 테스트 [값이 True면 성공 & 값이 False면 성공]

    @Test
    void test2() {
        assertTrue(calculator.validateNum(9));
        assertFalse(calculator.validateNum(0));
    }

 

4. assertNotNull & assertNull : Null값 테스트 [값이 Null이 아니면 성공 & 값이 Null이면 성공]

    @Test
    void test3() {
        Double result1 = calculator.operate(5, "/", 2);
        assertNotNull(result1);
        Double result2 = calculator.operate(5, "/", 0);
        assertNull(result2);
    }

 

5. assertThrows(Expect Exception, () -> method) : 예외 테스트 [예상한 예외가 맞으면 성공]

    @Test
    void test4() {
        IllegalArgumentException exception 
        = assertThrows(IllegalArgumentException.class, 
        () -> calculator.operate(5, "?", 2));
    }

 


□ Given - When - Then 패턴

  • given - 매개 변수 제공
  • when - 받은 매개 변수로 실행
  • then - 검증
 @Test
    @DisplayName("계산기 연산 성공 테스트")
    void test1() {
        // given
        int num1 = 5;
        String op = "/";
        int num2 = 2;

        // when
        Double result = calculator.operate(num1, op, num2);

        // then
        assertNotNull(result);
        assertEquals(2.5, result);
    }

□ Junit5 User Guide

https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations

 

JUnit 5 User Guide

Although the JUnit Jupiter programming model and extension model do not support JUnit 4 features such as Rules and Runners natively, it is not expected that source code maintainers will need to update all of their existing tests, test extensions, and custo

junit.org