Birim testi: Veritabanı mesajı
Dersteki ErrorStore ve InfoStore'u düşün. Diyelim ki process(String message) yöntemi bir [WARN] mesajı için kullanılıyor; bu da mesajın hiçbir veritabanına gitmeyeceği anlamına gelir. Bunu doğrulayan bir test yaz.
Bu egzersiz
Java'da Teste Giriş
kursunun bir parçasıdırEgzersiz talimatları
- Mesajın
infoStore'da olmadığını doğrula. - Ayrıca
times()doğrulama yaklaşımını kullanarakerrorStore'da da olmadığını doğrula. - Aynı ifadede, çağrılmayan veritabanı yöntemi
save()'i ekle.
Uygulamalı interaktif egzersiz
Bu örnek kodu tamamlayarak bu egzersizi bitirin.
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);
}
}