시작하기무료로 시작하기

단위 테스트: 리스트를 사용하는 데이터베이스

레슨에서 다룬 ErrorStoreInfoStore를 생각해 보세요. 이제 process() 메서드는 메시지 리스트를 입력으로 받습니다. 제공된 입력 리스트를 기준으로 각 데이터베이스가 올바른 횟수만큼 호출되었는지 검증하세요.

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

Java 테스트 소개

강의 보기

연습 안내

  • InfoStore에 대한 mock을 생성하세요.
  • ErrorStore에 대한 mock을 생성하세요.
  • 현재 인수로 InfoStore mock이 몇 번 호출되는지 검증하세요.
  • 현재 인수로 ErrorStore mock이 몇 번 호출되는지 검증하세요.

실습형 인터랙티브 연습

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

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

class MessageProcessorTest {
    @Test
    void process_savesToCorrectDatabase_whenValidInputList() {
        List sampleMessages = new ArrayList<>();
        sampleMessages.add("[INFO] An info message.");
        sampleMessages.add("[INFO] An info message.");
        sampleMessages.add("[ERROR] An error message.");
        sampleMessages.add("[INFO] An info message.");
        
    	// Create a mock for the InfoStore
        InfoStore ____ = ____(____.class);
        // Create a mock for the ErrorStore
        ErrorStore ____ = ____(____.class);
        MessageProcessor processor = new MessageProcessor(infoStore, errorStore);
        processor.process(sampleMessages);

		// Verify how many times the infoStore has to store the message
        ____(____, times(3)).save("[INFO] An info message.");
        // Verify how many times the errorStore has to store the message
        ____(____, times(1)).save("[ERROR] An error message."); 
    }
}
코드 편집 및 실행