티스토리 뷰
모든 소스는 여기서 확인하실 수 있습니다.
지난 포스팅에 이어서 JUnit5에서 추가된 내용을 간략하게 다뤄보려고 합니다.
Dependencies
ext {
junitJupiterVersion = '5.4.2'
junitPlatformVersion = '1.2.0'
junitVintageVersion = '5.4.2'
}
dependencies {
// junit5
testImplementation "org.junit.platform:junit-platform-runner:${junitPlatformVersion}"
testImplementation "org.junit.vintage:junit-vintage-engine:${junitVintageVersion}"
testImplementation "org.junit.jupiter:junit-jupiter-migrationsupport:${junitVintageVersion}"
testImplementation "org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}"
testImplementation "org.junit.jupiter:junit-jupiter-params:${junitJupiterVersion}"
testImplementation "org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}"
// lombok
testImplementation "org.projectlombok:lombok:1.18.10"
testAnnotationProcessor "org.projectlombok:lombok:1.18.10"
}
Test Suites
여러 테스트 클래스 모음을 같이 테스트할 수 있습니다. @SelectPackages, @SelectClasses 애노테이션을 통해 해당 기능을 제공합니다.
package io.lcalmsky.junit;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.runner.RunWith;
@RunWith(JUnitPlatform.class)
@SelectPackages("io.lcalmsky.junit") // 해당 패키지의 테스트를 모두 수행
public class IntegrationPackageTests {
}
package io.lcalmsky.junit;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.runner.RunWith;
@RunWith(JUnitPlatform.class)
@SelectClasses({AfterTests.class, AssertionTests.class, DisplayNameAndDisabledTests.class}) // 명시한 클래스들의 테스트를 수행
public class IntegrationClassTests {
}
Dynamic Tests
동적 테스트 기능으로 런타임시 생성된 테스트 케이스를 선언하고 실행할 수 있습니다. @TestFactory 애노테이션을 사용해 팩토리 메소드로 동적 테스트를 생성합니다.
package io.lcalmsky.junit;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestFactoryTests {
private List<String> in = new ArrayList<>(Arrays.asList("안녕", "응", "아니"));
private List<String> out = new ArrayList<>(Arrays.asList("Hello", "Yes", "No"));
@TestFactory
public Stream<DynamicTest> translateDynamicTestsFromStream() {
return in
.stream()
.map(word -> DynamicTest.dynamicTest("Test translate " + word, () -> {
int id = in.indexOf(word);
assertEquals(out.get(id), translate(word));
}));
}
private String translate(String word) {
if ("안녕".equalsIgnoreCase(word)) {
return "Hello";
} else if ("응".equalsIgnoreCase(word)) {
return "Yes";
} else if ("아니".equalsIgnoreCase(word)) {
return "No";
}
return "Error";
}
}
팩토리 메소드는 반드시 Stream, Collection, Iterable 또는 Iterator를 리턴해야합니다.
'Test' 카테고리의 다른 글
Mock은 Stub이 아니다(Mocks Aren't Stubs) (2) | 2022.05.11 |
---|---|
JUnit5 (1/2) (0) | 2019.11.26 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- proto3
- intellij
- spring boot application
- JPA
- Linux
- Jackson
- Java
- Spring Boot Tutorial
- 헥사고날 아키텍처
- 알고리즘
- Spring Boot JPA
- Spring Boot
- JSON
- 스프링 부트 튜토리얼
- leetcode
- @ManyToOne
- spring boot app
- 스프링 부트
- spring boot jwt
- gRPC
- 스프링 데이터 jpa
- 스프링 부트 애플리케이션
- 함께 자라기
- 함께 자라기 후기
- r
- 클린 아키텍처
- Spring Data JPA
- 스프링부트
- 스프링 부트 회원 가입
- QueryDSL
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함