27 Ocak 2023 Cuma

SpringScheduling @Scheduled Anotasyonu Unit Test

Örnek
Kod şöyle olsun
@Component
public class MyScheduler {

    private final MyService myService;

    @Scheduled(fixedDelayString = "${app.scheduler.delay-ms}")
    public void run() {
        myService.call();
    }
}
Şöyle yaparız
@ExtendWith(SpringExtension.class)
@SpringBootTest( classes = { MyApplication.class, MySchedulerIntegrationTest.MySchedulerTestConfig.class }, properties = { "app.scheduler.delay-ms=200" }) @ActiveProfiles("local") class MySchedulerIntegrationTest { // Dependency injected into scheduler, we mock it to verify is being called. @MockBean private MyService myService; @Test void shouldStartScheduler() { await().atMost(300, TimeUnit.MILLISECONDS) .untilAsserted(() -> { verify(myService, times(2)).call(); }); } @Configuration static class MySchedulerTestConfig { // If you need to mock anything in your context, add here using @MockBean. } }


Hiç yorum yok:

Yorum Gönder