시작하기무료로 시작하기

숫자 뒤집기에 TDD 적용하기

Test-Driven Development를 살펴봤으니, 이제 직접 적용해 보죠! int 값을 뒤집는 간단한 메서드를 생각해 보세요. TDD 관점에서 가장 먼저 해야 할 일은 테스트를 작성하는 것입니다. 구현이 아니라 메서드의 동작(입력과 출력)에 집중하세요. 이렇게 하면 프로젝트 요구사항을 먼저 고려하는 습관을 기를 수 있어요.

아래 코드 스니펫에서, 예상 출력값과 메서드의 실제 출력을 얻는 식, 그리고 assertEquals() 구문의 인자를 완성하세요.

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

Java 테스트 소개

강의 보기

연습 안내

  • 입력 숫자에 대한 올바른 뒤집힌 값을 입력하세요.
  • 실제 값을 얻기 위해 IntReverserreverse() 메서드를 호출하세요.
  • JUnit은 expected/actual 값을 특정 순서로 기대합니다. 이 순서에 맞게 assertEquals()에 입력하세요.

실습형 인터랙티브 연습

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

public class IntReverser {
    
    public static int reverse(int input) {
        return 0;
    }

    public static class IntReverserTest {
    
        @Test
        public void testReverse_reversesNumber() {
            int input = 1234;
            // Write down the expected return value
            int expected = ____;

			// Call the reverse() method to obtain its actual return value
            int actual = IntReverser.____(____);

			// Write the arguments for the assert statement in the correct order
            assertEquals(____, ____);
        }
    }

    public static void main(String[] args) {
		launchTestsAndPrint(IntReverserTest.class);
    }
}
코드 편집 및 실행