본문 바로가기
BackEnd/Java

[Java] JUnit5 애플리케이션을 테스트하는 다양한 방법 이야기 2부

by 뽀뽀이v 2020. 9. 22.

이 글은 백기선 님의 더 자바, 애플리케이션을 테스트하는 다양한 방법 강의를 듣고 복습 차원에서 적은 글입니다.

JUnit5 : 태킹과 필터링

  • 테스트 메소드에 태그를 추가할 수 있다.
  • 하나의 테스트 메소드에 여러 태그를 사용할 수 있다.
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
public class StudyTest {

    @Test
    @DisplayName("스터디 만들기 - fast")
    @Tag("fast")
    void create_new_study() {
        System.out.println("create fast");
        Study study = new Study(100);
        assertThat(study.getLimit()).isGreaterThan(0);
    }

    @Test
    @DisplayName("스터디 만들기 - slow")
    @Tag("slow")
    void create_new_study_again() {
        System.out.println("create slow");
    }

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

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

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

    @AfterEach
    // @Disabled // SKIP
    void afterEach() {
        System.out.println("after each");
    }

}

다음과 같이 @Test 메소드에 태그를 달아서 특정 태그만 실행을 할 수 있습니다.

// Gradle
test {
    useJUnitPlatform {
        includeTags 'fast'
        excludeTags 'slow'
    }
}

// Maven
<plugin>
	<artifactId>maven-surefire-plugin</artifactId>
	<configuration>
		<groups>fast</groups>
	</configuration>
</plugin>

JUnit5 : 커스텀 태그

  • JUnit5 애노테이션을 조합하여 커스텀 태그를 만들 수 있다.

FastTest.java

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME) // 런타임에서 유지 해야 한다.
@Test
@Tag("fast")
public @interface FastTest {
}

커스텀 태그를 활용하여 전략으로 사용할 수 있다.

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
public class StudyTest {

    @FastTest
    @DisplayName("스터디 만들기 - fast")
    // @Tag("fast")
    void create_new_study() {
        System.out.println("create fast");
        Study study = new Study(100);
        assertThat(study.getLimit()).isGreaterThan(0);
    }

    @SlowTest
    @DisplayName("스터디 만들기 - slow")
    // @Tag("slow")
    void create_new_study_again() {
        System.out.println("create slow");
    }

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

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

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

    @AfterEach
    // @Disabled // SKIP
    void afterEach() {
        System.out.println("after each");
    }

}

댓글