Closed 2jigoo closed 1 year ago
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter'
}
// task test: JUnit 5 플랫폼을 사용하도록 설정
test {
useJUnitPlatform()
}
org.junit.jupiter:junit-jupiter-api
, junit-jupiter-engine
모듈 사용@Test
가 붙은 메서드는 private이면 안 된다.Assertions.assertEquals
// 값 비교
assertEquals(expected, actual);
assertNotEquals(unexpected, actual);
// 레퍼런스 비교
assertSame(Object expected, Object actual);
assertNotSame(Object unexpected, Object actual);
assertTrue(boolean condition);
assertFalse(boolean condition);
assertNull(Object actual);
assertNotNull(Object actual);
// 테스트를 실패 처리
fail();
// executable을 실행한 결과, 지정한 타입의 Exception이 발생하는지 검사
assertThrows(Class<T> expectedType, Executable excutable);
// Exception이 발생하지 않는지 검사
assertDoesNotThrow(Executable executable);
// 발생한 Exception 객체를 리턴
IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> {
AuthService authService = new AuthService();
authService.authenticate(null, null);
});
// 발생한 Exception을 이용하여 *추가적인 검증*
assertTrue(thrown.getMessage().contains("id"));
Executable
package org.junit.jupiter.api.function;
public interface Executable {
void execute() throws Throwable;
}
assertThrows()
, assertDoesNotThrow()
에서 사용하는 함수형 인터페이스assertAll(
() -> assertEquals(3, 5/2),
() -> assertEquals(4, 2*2),
() -> assertEquals(6, 11/2)
);
@BeforeEach
, @AfterEach
@BeforeAll
, @AfterAll
@DisplayName
: 테스트 이름 표현@Disabled
: 테스트 실행 대상에서 제외mvn test
(mvnw test
)gradle test
(gradlew test
)given, when, then
예) 금융 회사에서 제공하는 REST API 사용 시, 자동이체 등록 기능
대역
: 테스트 대상이 의존하는 대상의 실제 구현을 대신하는 구현
Chapter 5. JUnit 5 기초 / Chapter 6. 테스트 코드의 구성
목표