BaşlayınÜcretsiz başlayın

Birim testi: Liste kullanan veritabanı

Dersteki ErrorStore ve InfoStore sınıflarını düşün; ancak artık process() metodumuz bir mesaj listesi alıyor. Verilen girdi listesine göre, her veritabanının doğru sayıda çağrıldığını doğrula.

Bu egzersiz, kursun bir parçasıdır

Java'da Teste Giriş

Kursa Göz Atın

Egzersiz talimatları

  • InfoStore için bir mock oluştur.
  • ErrorStore için bir mock oluştur.
  • Geçerli argümanlarla InfoStore mock'unun kaç kez çağrılacağını doğrula.
  • Geçerli argümanlarla ErrorStore mock'unun kaç kez çağrılacağını doğrula.

Uygulamalı etkileşimli egzersiz

Bu egzersizi bu örnek kodu tamamlayarak deneyin.

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."); 
    }
}
Kodu Düzenle ve Çalıştır