์์ฆ ์ผํ๋ฉด์ ๋ด๊ฐ ์ ์ผ ์ ๊ฒฝ ์ฐ๊ณ ์๋ ๊ฒ์ ํ์คํ ๋ฆฌ๋ฅผ ์ ๋จ๊ธฐ๋ ๊ฒ์ด๋ค
๋ชจ๋ ์ผ์ ๋๊ตฌ๋ ํ ์ ์๋ค๋ ๊ฒ์ ๊นจ๋ฌ์์ง๋ง
์ฝ๋๋ง๋ค ์๋ ์ฌ์ฐ์... ๊ทธ ์ฌ๋๋ฐ์ ์ ์๊ฐ ์๋ค๋ ๊ฒ์ด๋ค ๐ฅน
์ ๊ทธ๋ ๊ฒ ์๊ฐํ๊ฒ ๋๋๊ตฌ์? ์ ๋ ๊ทธ๋ฌ๊ณ ์ถ์ง ์์์ต๋๋ค
๊ทธ๋์!!
ํ ์คํธ ์ฝ๋๋ฅผ ์ฌ๋๋์ ๋ค๋ฆ์์ด ์์ฑํ๋ ์ค...
๊ธฐ์กด์๋ JUnit4 ๋ฒ์ ์ผ๋ก ์ฌ์ฉํ๊ณ ์์๋๋ฐ
๋ค๋ฅธ ๋ด๋น์๊ฐ ํ ์คํธ ์ฝ๋ ์คํ ์ ๋น ๋ฅด๊ฒ ๋ด์ฉ์ ํ์ ํ์๋ฉด ์ข๊ฒ ๋ค๊ณ ์๊ฐ์ด ๋ค์๋ค
๊ฒฐ๋ก ๋ถํฐ ๋จผ์ ๋งํ์๋ฉด..
ํ ์คํธ ์ฝ๋๋ฅผ ์คํํ์ ๋ ์ ์ด๋ฐ ํ ์คํธ~๋ฅผ ์ฝ๊ฒ ํ์ ํ๊ธธ ์ํ๋ค
๊ทธ๋ฌ๊ธฐ ์ํด์๋ @DisplayName ๊ฐ์ ์ด๋ ธํ ์ด์ ์ฌ์ฉ์ด ํ์ํ๊ณ ,
์ด๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ JUnit ๋ฒ์ ์ ์ ๊ทธ๋ ์ด๋ ํด์ผํ๋ค
1. JUnit5 ์์กด์ฑ ์ถ๊ฐ
(1) build.gradle
dependencies {
// JUnit 5 ์์กด์ฑ
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
// Mockito ์์กด์ฑ
testImplementation('org.mockito:mockito-core:3.12.4')
}
//...
test{
useJUnitPlatform()
}
(2) pom.xml
<!-- JUnit 5 ์์กด์ฑ -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.1</version> <!-- ์ต์ ๋ฒ์ ์ผ๋ก ๋์ฒดํด์ผ ํฉ๋๋ค. -->
<scope>test</scope>
</dependency>
<!-- Mockito ์์กด์ฑ -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.12.4</version> <!-- ์ต์ ๋ฒ์ ์ผ๋ก ๋์ฒดํด์ผ ํฉ๋๋ค. -->
<scope>test</scope>
</dependency>
2. JUnit4 vs JUnit5
https://junit.org/junit5/docs/current/user-guide/
JUnit 5 User Guide
Although the JUnit Jupiter programming model and extension model do not support JUnit 4 features such as Rules and Runners natively, it is not expected that source code maintainers will need to update all of their existing tests, test extensions, and custo
junit.org
๊ณต์ ๋ฌธ์๊ฐ ์ ๋ฆฌ๊ฐ ์ ๋์ด์์ด์ ์๋ ์์ ์ธ์ฉํ๋ ค๊ณ ํฉ๋๋ค
๋์ ์ฐจ์ด์ ์ ํฌ๊ฒ ์๋๋ก ์ ๋ฆฌํด๋ณผ ์ ์๋ค
Java ๋ฒ์ ์ ๋ช ๋ถํฐ ์ง์ํ๋์ง?
๊ตฌ์กฐ๊ฐ ์ด๋ป๊ฒ ๋ฌ๋ผ์ก๋์ง?
์ฌ์ฉ๋ฒ์ด ์ด๋ป๊ฒ ๋ฌ๋ผ์ก๋์ง?
(1) Java ๋ฒ์
Java 8 ๋๋ ๊ทธ ์ด์์ ์จ์ผํ๋ค๊ณ ๋์ด์๋๋ฐ์, ํ ์คํธํ๋๋ฐ๋ ์ด์ ๋ฒ์ ์ด์ด๋ ๊ด์ฐฎ์ต๋๋ค
JUnit 5 requires Java 8 (or higher) at runtime. However, you can still test code that has been compiled with previous versions of the JDK.
๐งJUnit 5๋ ๋ฐํ์ ์ Java 8(๋๋ ๊ทธ ์ด์)์ด ํ์ํฉ๋๋ค. ๊ทธ๋ฌ๋ ์ด์ ๋ฒ์ ์ JDK๋ก ์ปดํ์ผ๋ ์ฝ๋๋ฅผ ๊ณ์ ํ ์คํธํ ์ ์์ต๋๋ค.
(2) ๊ตฌ์ฐจํ..๊ตฌ์กฐ ์ฐจ์ด
JUnit4๋ ์ฌ์ธ์ ํ ๋ ๊ฐ์ ๋ ์์ ๋๋ค.
๊ทผ๋ฐ JUnit5๋ ํ๋ซํผ์ ๊ตฌ์ฑ์ด ์ถ๊ฐ ๋์ต๋๋ค
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
JUnit Platform
JVM์์ ํ ์คํธ ํ๋ ์์ํฌ ์คํํ๋ ๊ธฐ๋ณธ์ด ๋๋ ๋ ์์ ๋๋ค
TestEngine API๋ฅผ ์ฌ์ฉํ ์ ์๋๋ก ํด์ค๋๋ค
Console Lancher๋ฅผ ์ ๊ณตํ๋ ๊ฒ๋ ์ด ๋ ์์ ๋๋ค
IntelliJ ๊ฐ์ IDE์์๋ ์ฌ์ฉํ ์ ์์ต๋๋ค
JUnit Jupiter ๐ช
(์ ๋ชฉ์ฑ ์ด๋ชจ์ง๋ ์์ฃ )
์ด ๋ ์์ ํ์ฅํฉ ๊ฐ์ ์น๊ตฌ์ ๋๋ค
JUnit 5์์ ํ ์คํธ๋ฅผ ์์ฑํ๊ณ ํ์ฅ์ ํ๊ธฐ ์ํ
์๋ก์ด ํ๋ก๊ทธ๋๋ฐ ๋ชจ๋ธ๊ณผ ํ์ฅ ๋ชจ๋ธ์ ์ข ํฉ ์ธํธ์ ๋๋ค
JUnit Vintage
JUnit3๊ณผ JUnit4 ๊ธฐ๋ฐ ํ ์คํธ๋ฅผ ์คํํ๊ธฐ ์ํ TestEngine์ ์ ๊ณตํฉ๋๋ค
๊ฐ์ธ์ ์ผ๋ก ์ด๋ฆ ๋๊ฒ ์ ์ง์๋ค ์๊ฐํจ
(3) Annotation ์ฌ์ฉ
JUnit 4 | JUnit 5 | ์ค๋ช |
@Test | @Test | ๋๋์ผ ํ ์คํธ ๋ฉ์๋ |
N/A | @ParameterizedTest | ๋งค๊ฐ ๋ณ์ํ๋ ํ ์คํธ |
N/A | @RepeatedTest | ๋ฐ๋ณต ํ ์คํธ ์ ์ฌ์ฉ |
N/A | @TestFactory | ๋์ ํ ์คํธ๋ฅผ ์ํ ํ ์คํธ ํฉํ ๋ฆฌ |
N/A | @TestTemplate | ์ปจํ ์คํธ ์ฌ๋ฌ ๊ฐ๋ฅผ ํธ์ถํ์ฌ ์ฌ๋ฌ ๋ฒ ํ ์คํธํ ์ ์์ |
N/A | @TestClassOrder | ํ ์คํธํ ํด๋์ค ์์ ์ ํ ์ ์๋ค |
@FixMethodOrder | @TestMethodOrder | ๋ฉ์๋ ์คํ ์์๋ฅผ ์ ํ ์ ์๋ค |
N/A | @TestInstance | ํ ์คํธ ์ธ์คํด์ค ์๋ช ์ฃผ๊ธฐ๋ฅผ ์ค์ |
N/A | @DisplayName | ํ ์คํธ ํด๋์ค๋ ๋ฉ์๋ ๋ณด์ฌ์ง ์ด๋ฆ ์ง์ |
N/A | @DisplayNameGeneration | ํ ์คํธ ํด๋์ค์ ๋ณด์ฌ์ง ์ด๋ฆ ์ง์ |
@BeforeClass | @BeforeAll | ํ์ฌ ํด๋์ค์ ๋ชจ๋ ํ ์คํธ ๋ฉ์๋๋ณด๋ค ๋จผ์ ์คํ |
@AfterClass | @AfterAll | ํ์ฌ ํด๋์ค์ ๋ชจ๋ ํ ์คํธ ๋ฉ์๋ ์คํ ํ ์คํ |
@Before | @BeforeEach | ๊ฐ ํ ์คํธ ๋ฉ์๋ ์ ์ ์คํ |
@After | @AfterEach | ๊ฐ ํ ์คํธ ๋ฉ์๋ ํ์ ์คํ |
@Ignore | @Disabled | ํ ์คํธ ๋ฉ์๋/ํด๋์ค ๋นํ์ฑํ ํ ์ ์์ธฐ |
N/A | @TestFactory | ๋์ ํ ์คํธ๋ฅผ ์ํ ํ ์คํธ ํฉํ ๋ฆฌ |
N/A | @Nested | ์ค์ฒฉ ํ ์คํธ |
@Category | @Tag | ํ๊น (Tagging) ๋ฐ ํํฐ๋ง(Filtering) |
N/A | @ExtendWith | ์ฌ์ฉ์ ์ ์ ํ์ฅ ๋ฑ๋ก |
N/A | @Timeout | ์คํ ์ง์ ๊ธฐ๊ฐ์ ์ด๊ณผํ๋์ง ํ ์คํธํ ์ ์์ |
N/A | @RegisterExtension | ํ์ฅ ๋ฑ๋ก |
N/A | @TempDir | ์์ ๋๋ ํฐ๋ฆฌ ์์ฑํ๋๋ฐ ์ฌ์ฉ |
3. ์ฌ์ฉ ์์
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("A special test case")
class DisplayNameDemo {
@Test
@DisplayName("Custom test name containing spaces")
void testWithDisplayNameContainingSpaces() {
}
@Test
@DisplayName("โฏ°โก°๏ผโฏ")
void testWithDisplayNameContainingSpecialCharacters() {
}
@Test
@DisplayName("๐ฑ")
void testWithDisplayNameContainingEmoji() {
}
}
์ด๋ชจ์ง๋ ์ฌ์ฉํ๋ ์ด ๋ ์ ์ ๋ฒ ๊ท์ฝ์ต๋๋ค
๋๋จธ์ง ์ด๋ ธํ ์ด์ ๋ JUnit ์์ ๊ฐ ์์ฃผ ์ ๋์์๋ค์
'๐ Java&Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํจ์ํ ์ธํฐํ์ด์ค (Functional Interface) (2) | 2023.11.25 |
---|---|
NonNull ์ฌ๋...์ฌ...์ฌ...๋... ๊ทธ๋ฅ ์ข์ํฉ๋๋ค. (3) | 2023.11.07 |
Error) java: class, interface, or enum expected ํด๊ฒฐ (0) | 2023.06.26 |
๐ธ startsWith()/endWith() ๋ฌธ์์ด์ ์์๊ณผ ๋ (0) | 2023.04.04 |
@JsonProperty์ @JsonNaming (0) | 2023.03.17 |