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

BeforeEach: Uyarı Verme

Projenin karmaşıklığı arttıkça, testlerin kurulumu giderek daha uzun sürebilir. Bunu öngörerek DurationMonitorTeste geri dönüyor ve bazı test kurulumlarını setup() metodunda toplamak için yeni anotasyonları kullanıyorsun.

Kurulumun her testten önce çalışması için @BeforeEach kullan.

Bu egzersiz

Java'da Teste Giriş

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

  • setup() metodunu doğru anotasyonla belirt.
  • İçine gerekli kurulumu yaz: AlertService bir mock olacak, DurationMonitor bu mock'u alacak.

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

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

class DurationMonitorTest {
	private AlertService alertService;
	private DurationMonitor monitor;
    
    // Use the correct annotation to make this method execute before every test
    @____
    void setUp() {
    	// Set up the mock alertService and the monitor as you did in the earlier exercise
    	this.alertService = ____(____.class);
        this.monitor = new ____(alertService);
    }
    
    @Test
    void recordDuration_triggersAlert_whenAboveLimit() {
        this.monitor.recordDuration(1500);
        verify(this.alertService).trigger("Slow execution detected: 1500ms");
    }

    @Test
    void recordDuration_doesNotTriggerAlert_whenUnderLimit() {
        this.monitor.recordDuration(500);
        verifyNoInteractions(this.alertService);
    }
}
Kodu Düzenle ve Çalıştır