시작하기무료로 시작하기

CsvSource: 숫자

1장에서 다뤘던 lastDigit()를 다시 살펴보고, 이번에는 Math.abs()를 사용해 음수도 처리하도록 대비했어요. 이제 @ParameterizedTest로 테스트를 간결하게 만들 수 있으니, lastDigit()의 모든 결과를 한 번에 검증하려고 해요.

양수, 음수, 그리고 0으로 끝나는 정수에 대해 메서드가 올바른 한 자리 수를 반환하는지 확인하세요.

이 연습은 강의의 일부입니다

Java 테스트 소개

강의 보기

연습 안내

  • 하나의 테스트 메서드에 여러 값을 입력하는 애너테이션을 사용하세요.
  • 테스트 메서드에 필요한 메서드 인자를 추가하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

public class LastDigitWithTests {
  public static void main(String[] args) {
    launchTestsAndPrint(LastDigitTest.class);
  }
}

class LastDigit {
    public static int lastDigit(int a) {
        return Math.abs(a) % 10;
    }
}

class LastDigitTest {

    @ParameterizedTest
    // Use the correct annotation for a pair of integer inputs
    @____(value = {"2025, 5", "-2025, 5", "2020, 0"}) 
    // Write the corresponding argument types for the test method
    void testLastDigit(____ number, ____ expected) {
        int actual = LastDigit.lastDigit(number);

        assertEquals(expected, actual);
    }
}
코드 편집 및 실행