JUnit5.araboja

나르·2021년 10월 10일
0

JAVA

목록 보기
8/18
post-thumbnail

📗 JUnit5 📕

Architecture

" The new major version of the programmer-friendly testing framework for Java 8 "

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

기존의 JUnit 버전들과는 달리, JUnit 5는 서로 다른 하위 프로젝트로부터 기원한 서로 다른 모듈의 집합이다.
JUnit 5는 실행 시 Java 8, 또는 그 이상 버전을 필요로 한다.

  • JUnit Platform
    JUnit Platform은 JVM 기반 테스팅 프레임워크 를 실행시키기 위한 기반 모듈이다.
    플랫폼 모듈은 플랫폼 기반으로 실행 가능한 모든 테스팅 을 위해 TestEngine API를 제공하며, 이러한 테스팅을 Gradle, Maven과 연동 가능하게 하는 Console Launcher를 제공한다.
    플랫폼 모듈은 또한, JUnit 4 환경 하에서 플랫폼 모듈을 이용하여 진행하는 테스트를 위해 Runner를 제공한다.

  • JUnit Jupiter
    JUnit Jupiter는 JUnit 5 기반 테스트 케이스를 작성하기 위한 프로그래밍 모델과 확장 모델을 지원하는 모듈로, 테스트 작성자를 위한 junit-jupiter-api 모듈과 테스트 실행을 위한 junit-jupiter-engine가 분리되어 있다.
    주피터 모듈은 주피터 기반으로 작성된 테스트 케이스를 JUnit Platform에서 실행시키기 위한 TestEngine 역시 제공한다.

  • JUnit Vintage
    JUnit Vintage는 하위호환성을 위해 JUnit3, JUnit4 기반의 테스트를 JUnit Platform에서 실행시키기 위한 TestEngine을 제공하는 모듈이다.

Jupiter API: assertions

기본 assert 메서드, fail() 메서드

org.junit.jupiter.api.Assertions 클래스는 검증을 위한 static 메서드를 제공하고 있다. 주요 메서드는 다음과 같다.

  • assertEquals(expected, actual)
  • assertNotEquals(Object unexpected, Object actual)
  • assertTrue(boolean condition)
  • assertFalse(boolean condition)
  • assertNull(Object actual)
  • assertNotNull(Object actual)
  • fail()

assertAll()

여러 검증을 시도할 때, 첫 번째 assertEquals()가 실패하면 그 시점에서 테스트 실패이므로 이후의 assertEquals()를 실행하지 않는다.
assertAll()은 여러 검증을 하나로 묶어서 테스트 할 수 있게 해준다.

public class AssertAllTest {
    @Test
    void assertAllSample() {
        Game game = new Game(123);
        Score score = game.guess(145);
        assertAll(
                () -> assertEquals(2, score.getStrikes()),
                () -> assertEquals(1, score.getBalls())
        );
    }
}

assertThrows()

실행한 코드에서 특정 익셉션이 발생하는지 확인할 때에는 assertThrows() 메서드를 사용한다.

@Test
void simple() {
    assertThrows(ArithmeticException.class, () -> divide(100, 0));
}

private int divide(int op1, int op2) {
    return op1 / op2;
}

Jupiter API: 테스트 라이프사이클

public class LifecycleTest {

    @BeforeAll // JUnit4의 @BeforeClass
    static void initAll() {
        System.out.println("테스트 Class 기준으로 테스트 메서드들이 실행되기전 실행");
    }

    @BeforeEach // JUnit4의 @Before
    void init() {
        System.out.println("각 테스트 메서드가 실행되기전 실행");
    }

    @Test
    void Test() {
        System.out.println("테스트 메서드");
    }

    @AfterEach // JUnit4의 @After
    void tearDown() {
        System.out.println("각 테스트 메서드가 실행된 후 실행");
    }

    @AfterAll // JUnit4의 @AfterClass
    static void tearDownAll() {
        System.out.println("테스트 Class 기준으로 테스트 메서드들이 실행된 후 실행");
    }
}

Dependencies

  • JUnit Platform
아이디설명
junit-platform-commonsJUnit 내부적으로 사용하는 공통 라이브러리(혹은 유틸리티). JUnit 모듈 외부에서 사용되는 어떠한 상황도 상정하고 있지 않음.
junit-platform-console콘솔을 사용하여 JUnit Platform 상에서 수행되는 테스트를 위한 지원 라이브러리
junit-platform-console-standalone ConsoleLauncher 의 실행 가능한 JAR 파일
junit-platform-engineJUnit 5 기반의 테스트 수행을 위한 API
junit-platform-gradle-pluginGradle을 사용한 플랫폼 상 테스트를 위한 지원 라이브러리
junit-platform-launcher테스트 플랜을 구성하고 실행하는 API - IDE 및 빌드 도구에서 활용
junit-platform-runnerJUnit 4 환경에서 플랫폼 이용 테스트를 지원하기 위한 러너
junit-platform-suite-apitest suite 구성을 위한 어노테이션
junit-platform-surefire-providerMaven Surefire 지원 라이브러리
  • JUnit Jupiter
아이디설명
junit-jupiter-api테스트 작성 및 확장(Runner 등)을 위한 JUnit Jupiter API
junit-jupiter-engine주피터 모듈의 TestEngine 구현체
junit-platform-console-standalone 인자가 있는 테스트를 위한 지원 라이브러리
junit-platform-engineJUnit4에서 주피터 모듈로의 마이그레이션 지원 라이브러리
  • JUnit Vintage
아이디설명
junit-vintage-engineJUnit Vintage의 TestEngine 구현체. JUnit3 또는 4에서 작성된 테스트를 JUnit Platform에서 실행시킬 때 사용

Reference.

https://junit.org/junit5/docs/current/user-guide/
https://stackoverflow.com/questions/38822189/why-were-junit-jupiter-and-junit-vintage-separated-when-i-running-testcase-in-in
https://javacan.tistory.com/entry/JUnit-5-Intro
https://awayday.github.io/2017-10-29/junit5-02/https://donghyeon.dev/junit/2021/04/11/JUnit5-완벽-가이드/

profile
💻 + ☕ = </>

0개의 댓글