시작하기무료로 시작하기

BeforeEach: 알림 처리

프로젝트가 복잡해질수록 테스트 준비 작업이 점점 길어질 수 있습니다. 이를 대비해 DurationMonitorTest를 다시 살펴보고, 새로운 애너테이션을 사용해 일부 설정을 setup() 메서드로 분리하려고 합니다.

각 테스트 전에 설정이 실행되도록 @BeforeEach를 사용하세요.

이 연습은 강의의 일부입니다

Java 테스트 소개

강의 보기

연습 안내

  • setup() 메서드에 올바른 애너테이션을 추가하세요.
  • 필요한 설정을 메서드 안에 작성하세요: 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);
    }
}
코드 편집 및 실행