Skip to content

Commit

Permalink
added parametrized schedule, with test application.properties for spr…
Browse files Browse the repository at this point in the history
  • Loading branch information
glelouet committed May 25, 2024
1 parent af8ee09 commit ec02ae8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
11 changes: 11 additions & 0 deletions src/main/java/com/example/schedulingtasks/ScheduledTasks.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

Expand All @@ -35,4 +36,14 @@ public class ScheduledTasks {
public void reportCurrentTime() {
log.info("The time is now {}", dateFormat.format(new Date()));
}

@Value("${my.app.time.report.skip:false}")
private boolean skipReportTime = false;

@Scheduled(fixedRateString = "${my.app.time.report.cycle:8000}", initialDelayString = "${my.app.time.report.delay:0}")
public void reportCurrentTimeParametered() {
if (!skipReportTime) {
log.info("[parametered] The time is now {}", dateFormat.format(new Date()));
}
}
}
18 changes: 13 additions & 5 deletions src/test/java/com/example/schedulingtasks/ScheduledTasksTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@

package com.example.schedulingtasks;

import static org.awaitility.Awaitility.await;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.verify;

import org.awaitility.Durations;
import org.junit.jupiter.api.Test;

import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.SpyBean;

import static org.awaitility.Awaitility.await;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.verify;
import org.springframework.test.context.ActiveProfiles;

@SpringBootTest
@ActiveProfiles("config")
public class ScheduledTasksTest {

@SpyBean
Expand All @@ -38,4 +39,11 @@ public void reportCurrentTime() {
verify(tasks, atLeast(2)).reportCurrentTime();
});
}

@Test
public void reportCurrentTimeParametered() {
await().atMost(Durations.TEN_SECONDS).untilAsserted(() -> {
verify(tasks, atLeast(2)).reportCurrentTimeParametered();
});
}
}
7 changes: 7 additions & 0 deletions src/test/resources/application-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
my:
app:
time:
report:
cycle: 3000
delay: 1000
# skip: true

0 comments on commit ec02ae8

Please sign in to comment.