शुरू करेंमुफ़्त में शुरू करें

BeforeEach: अलर्टिंग

जैसे-जैसे आपका प्रोजेक्ट जटिल होता है, टेस्ट का सेटअप लंबा होता जा सकता है। इसे ध्यान में रखते हुए, आप DurationMonitorTest पर लौटते हैं और कुछ टेस्ट सेटअप को setup() मेथड में निकालने के लिए नई annotations का उपयोग करते हैं.

@BeforeEach का उपयोग करें ताकि हर टेस्ट से पहले सेटअप चले.

यह अभ्यास पाठ्यक्रम का हिस्सा है

Java में Testing परिचय

पाठ्यक्रम देखें

अभ्यास निर्देश

  • setup() मेथड को सही annotation से annotate करें.
  • अंदर आवश्यक सेटअप लिखें: AlertService एक mock है, DurationMonitor उसी mock को लेता है.

इंटरैक्टिव व्यावहारिक अभ्यास

इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।

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);
    }
}
कोड संपादित करें और चलाएँ