3

I've got scheduler with cron as follows:

@Scheduled(cron = "0 0 1 * * *")
@SchedulerLock(name = "PT20M")
public void schedulerSubscriptionsUpdate() {}

I'm trying to test entire functionality of this scheduled method - some kind of an integration test (including correctness of method's execution - time). Do you have any sugestions how can I do this?

nodlaw
  • 39
  • 2

1 Answers1

1

You can use Awaility library which provides for this purpose:

Add that library with maven:

<dependency>
    <groupId>org.awaitility</groupId>
    <artifactId>awaitility</artifactId>
    <version>3.1.6</version>
    <scope>test</scope>
</dependency>

We can use the Awaitility DSL to make our test more declarative, Something like:

@SpringJUnitConfig(ScheduledConfig.class)
public class ScheduledAwaitilityIntegrationTest {

    @SpyBean
    private Counter counter;

    @Test
    public void whenWaitOneSecond_thenScheduledIsCalledAtLeastTenTimes() {
        await()
          .atMost(Duration.ONE_SECOND)
          .untilAsserted(() -> verify(counter, atLeast(10)).scheduled());
    }
}

Source - baeldung.com/spring-testing-scheduled-annotation

Pramod S. Nikam
  • 4,271
  • 4
  • 38
  • 62