시작하기무료로 시작하기

단위 테스트: Database 메시지

수업에서 다룬 ErrorStoreInfoStore를 떠올려 보세요. process(String message) 메서드가 [WARN] 메시지에 사용된다고 가정해 봅시다. 이 경우 메시지는 어느 데이터베이스에도 저장되지 않아야 합니다. 이를 검증하는 테스트를 작성하세요.

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

Java 테스트 소개

강의 보기

연습 안내

  • 메시지가 infoStore에 저장되지 않았음을 검증하세요.
  • times()를 사용하는 검증 방식으로 errorStore에도 저장되지 않았음을 확인하세요.
  • 같은 구문에서 호출되지 않은 데이터베이스 메서드 save()를 함께 명시하세요.

실습형 인터랙티브 연습

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

public class Main {
    public static void main(String[] args) {
        launchMockitoTestsAndPrint(MessageProcessorTest.class);
    }
}

class MessageProcessorTest {

    @Test
    void process_savesNowhere_whenWrongMessageType() {
        InfoStore infoStore = mock(InfoStore.class);
        ErrorStore errorStore = mock(ErrorStore.class);
        MessageProcessor processor = new MessageProcessor(infoStore, errorStore);
        String message = "[WARN] The search is slow.";

        processor.process(message);

        // Verify infoStore was not called
        ____(infoStore);
        // Verify errorStore.save was called 0 times
        verify(errorStore, ____(0)).____(message);
    }
}
코드 편집 및 실행