JUnit5

2019-12-11

2018년 초에 프로젝트를 진행하면서 JUnit5 관련 스터디 한 자료가 있어서
블로그로 옮겨본다.

JUnit5

JUnit은 Java 단위테스트 작성을 위한 표준 framework
기존 우리가 사용하던버전은 JUnit4로 새로나온 JUnit5를 알아 볼께요

차이점

image01

JUnit Platform

JUnit 플랫폼은 JVM에서 테스트 프레임 워크를 시작하기위한 기초 역할을합니다.
또한 플랫폼에서 실행되는 테스트 프레임 워크를 개발하기위한 TestEngine API를 정의합니다.
또한 플랫폼은 명령 행에서 플랫폼을 실행하고 Gradle 및 Maven 용 플러그인을 빌드하는 Console Launcher를 제공하며
플랫폼에서 TestEngine을 실행하기위한 JUnit 4 기반 Runner도 제공합니다.

JUnit Jupiter

JUnit 5에서 테스트 및 확장 작성을위한 새로운 프로그래밍 모델과 확장 모델의 조합입니다.
Jupiter 하위 프로젝트는 플랫폼에서 Jupiter 기반 테스트를 실행하기위한 TestEngine을 제공합니다.

JUnit Vintage

플랫폼에서 JUnit 3 및 JUnit 4 기반 테스트를 실행하기위한 TestEngine을 제공합니다.

Maven Dependency

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

<!--JUnit 5 Dependency-->
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.0.1</version>
</dependency>
<!--JUnit 4 IDE Compatibility Dependencies-->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>4.12.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

JUnit4 와 달라진점

assert

assertThrows

보조 Annotation @DisplayName @Disabled

Assumption

Nested

참고

http://junit.org/junit5/docs/current/user-guide/
http://javacan.tistory.com/entry/JUnit-5-Intro?category=454313