티스토리 뷰

 

1. 테스트 인스턴스

  - 테스트 메소드는 테스트 클래스의 인스턴스를 각각 생성한다.

  - 테스트 각각의 의존성이 없음

int value = 1;

@Test
@DisplayName("스터디 만들기")
void create_study() {
    System.out.println(value++);
    System.out.println(this);
}

@Test
@DisplayName("스터디 만들기")
void create_study_again() {
    System.out.println(value++);
    System.out.println(this);
}

>> 실행 결과

 

 

 

2. @TestInstance : 테스트 인스턴스의 생명주기를 결정

  - @TestInstance(TestInstance.Lifecycle.PER_CLASS)

  - 테스트 인스턴스의 라이프 사이클을 클래스 단위로 생성되도록 설정

  - 테스트 메소드가 하나의 인스턴스를 공유

  - @BeforeAll, @AfterAll 테스트 메소드를 static으로 선언하지 않아도 된다.

    (클래스의 인스턴스가 하나이고 모든 메소드가 공유하기 때문에)

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@DisplayName("스터디 클래스 테스트")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class StudyTest {
    int value = 1;

    @Test
    @DisplayName("스터디 만들기 1")
    void create_study_1() {
        System.out.println(value++);
        System.out.println(this);
    }

    @Test
    @DisplayName("스터디 만들기 2")
    void create_study_2() {
        System.out.println(value++);
        System.out.println(this);
    }

    @BeforeAll
    static void beforeAll() {
        System.out.println("beforeAll");
    }

    @AfterAll
    static void afterAll() {
        System.out.println("afterAll");
    }
}

>> 실행 결과

 

3. @TestMethodOrder : 테스트 순서를 설정

  - @TestMethodOrder(MethodOrderer.OrderAnnotation.class)

  - 단위 테스트 각각 의존성을 낮춰야 하지만

    시나리오 테스트 등 테스트 간의 의존성을 갖고 순서대로 실행해야 하는 경우 사용

  - 인스턴스 하나를 공유해 순서대로 실행되야 하는 테스트라면

    @TestInstance(TestInstance.Lifecycle.PER_CLASS) 를 함께 사용

 

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@DisplayName("스터디 클래스 테스트")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class StudyTest {
    int value = 1;

    @Order(4)
    @Test
    @DisplayName("스터디 만들기 1")
    void create_study_1() {
        System.out.println(value++);
        System.out.println(this);
    }

    @Order(3)
    @Test
    @DisplayName("스터디 만들기 2")
    void create_study_2() {
        System.out.println(value++);
        System.out.println(this);
    }

    @Order(2)
    @Test
    @DisplayName("스터디 만들기 3")
    void create_study_3() {
        System.out.println(value++);
        System.out.println(this);
    }

    @Order(1)
    @Test
    @DisplayName("스터디 만들기 4")
    void create_study_4() {
        System.out.println(value++);
        System.out.println(this);
    }
}

>> 실행 결과

 

 

 

 

출처

https://www.inflearn.com/course/the-java-application-test 더 자바, 애플리케이션을 테스트하는 다양한 방법(백기선)

728x90