트밀 2023. 1. 15. 21:56

Junit은 1997년도 켄트 벡에 의해 개발되었다.

X-Unit (스몰토크 유닛테스팅 도구인 SUnit(이것 또한 켄트벡의 작품)과 비슷한 구조를 가진 것)중 하나인 라이브러리이다. 

자바 라이브러리로서는 압도적 1등이다. 

전자는 4버전, 후자는 5버전이다. 

Junit4가 더많이 쓰이긴 하지만,  Junit5 또한 많이 사용되고 있는데, 차이점이 무엇일까라는 점에 착안해, Junit의 개념들을 포함해 공부하기로 한다. 

 

Junit5

5부터는 Jupiter라는 이름이 쓰인다. 

JUnit is one of the most popular unit-testing frameworks in the Java ecosystem. The JUnit 5 version contains a number of exciting innovations, with the goal of supporting new features in Java 8 and above, as well as enabling many different styles of testing.

 

문법적 차이

구조(3개의 모듈)

Architectural building blocks of Junit5

 

플랫폼 -돌아가는 플랫폼 그리고  TestEngine API

주피터 - This module includes new programming and extension models for writing tests in JUnit 5. New annotations in comparison to JUnit 4 are:

빈티지 - JUnit Vintage supports running tests based on JUnit 3 and JUnit 4 on the JUnit 5 platform.

실행흐름

어노테이션

람다를 활용한 어썰션과 어썸션

we can now use lambdas in assertions,assumption: lazily evaluated,, which can save time and resources if the message construction is expensive.

가. 어썰션 - group assertions with assertAll(), 

나. 어썸션

 

Exception Testing

@Test
void shouldThrowException() {
    Throwable exception = assertThrows(UnsupportedOperationException.class, () -> {
      throw new UnsupportedOperationException("Not supported");
    });
    assertEquals("Not supported", exception.getMessage());
}

@Test
void assertThrowsException() {
    String str = null;
    assertThrows(IllegalArgumentException.class, () -> {
      Integer.valueOf(str);
    });
}

Test Suites(ide가 대부분 지원안함)

@SelectPackages and @SelectClasses

 

 

 

 Dynamic Tests

at run-time

@TestFactory. 

 

참고

JUnit 4 vs JUnit 5. (2022, February 3). Taogen's Blog. https://www.taogenjia.com/2022/02/03/JUnit-4-vs-JUnit-5/

baeudung/junit5

A Guide to JUnit 5. (2019, July 13). Baeldung. https://www.baeldung.com/junit-5