LoslegenKostenlos loslegen

Unit test: Database message

Consider the ErrorStore and InfoStore from the lesson. Suppose the process(String message) method is used on a [WARN] message, which means it would end up in neither database. Write a test to verify that.

Diese Übung ist Teil des Kurses

Introduction to Testing in Java

Kurs anzeigen

Anleitung zur Übung

  • Verify the message did not end up in infoStore.
  • Verify it also did not end up in errorStore using the times() verify approach.
  • In that same statement, add the database method, save(), that wasn't called.

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

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);
    }
}
Code bearbeiten und ausführen