본문 바로가기
T.I.L. :: Today I Learned

230721 T.I.L

by DaSsom 2023. 7. 21.
@MethodSource
@ParameterizedTest

프로젝트 완성 강의 듣다가 새로운 테스트코드 구현 방법을 알게 되어 정리해봄.

 

https://dublin-java.tistory.com/56

 

JUnit 5 Parameterized Tests 사용하기

혹시 테스트 코드를 작성할 때 아래와 같은 중복되는 코드를 작성하고 계신가요? @Test @DisplayName("User 생성 name 2자 미만 예외처리") void createUserException01() { IllegalArgumentException e = assertThrows(IllegalArgu

dublin-java.tistory.com

 

@ParameterizedTest 를 사용하면 하나의 테스트 메소드로 여러 개의 파라미터에 대해서 테스트할 수 있으므로 테스트코드의 중복을 줄여줌

 

@MethodSource를 사용하여 복잡한 오브젝트를 전달할 수 있음

@ParameterizedTest
@MethodSource("provideStringsForIsBlank")
void isBlank_ShouldReturnTrueForNullOrBlankStrings(String input, boolean expected) {
     assertEquals(expected, Strings.isBlank(input));
}

private static Stream<Arguments> provideStringsForIsBlank() {
    return Stream.of(
       Arguments.of(null, true)
       Arguments.of("", true),
       Arguments.of("  ", true),
       Arguments.of("not blank", false)
    );
}

이런 식으로 메서드를 정의해주고 그걸 불러서 사용할 수 있음.

'T.I.L. :: Today I Learned' 카테고리의 다른 글

🪄 230815 인프콘 2023 다녀왔다!  (0) 2023.09.03