BeforeEach: оповещения
По мере роста проекта настройка тестов может становиться всё более громоздкой. Предвидя это, вы возвращаетесь к DurationMonitorTest и выносите часть кода настройки в отдельный метод setup() с помощью новых аннотаций.
Используйте @BeforeEach, чтобы метод настройки выполнялся перед каждым тестом.
Это упражнение является частью курса
Введение в тестирование на Java
Инструкции к упражнению
- Добавьте к методу
setup()нужную аннотацию. - Пропишите внутри необходимую настройку:
AlertServiceдолжен быть моком, аDurationMonitor— принимать этот мок в качестве аргумента.
Интерактивное практическое упражнение
Попробуйте выполнить это упражнение, дополнив этот пример кода.
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);
}
}