From ec02ae85b4b667ff004d7b2160bb2dc0c241ae0e Mon Sep 17 00:00:00 2001 From: glelouet Date: Sat, 25 May 2024 18:31:50 +0200 Subject: [PATCH] added parametrized schedule, with test application.properties for #30 --- .../schedulingtasks/ScheduledTasks.java | 11 +++++++++++ .../schedulingtasks/ScheduledTasksTest.java | 18 +++++++++++++----- src/test/resources/application-config.yaml | 7 +++++++ 3 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 src/test/resources/application-config.yaml diff --git a/src/main/java/com/example/schedulingtasks/ScheduledTasks.java b/src/main/java/com/example/schedulingtasks/ScheduledTasks.java index dc039b1..fb1177d 100644 --- a/src/main/java/com/example/schedulingtasks/ScheduledTasks.java +++ b/src/main/java/com/example/schedulingtasks/ScheduledTasks.java @@ -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; @@ -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())); + } + } } diff --git a/src/test/java/com/example/schedulingtasks/ScheduledTasksTest.java b/src/test/java/com/example/schedulingtasks/ScheduledTasksTest.java index 552811c..203948d 100644 --- a/src/test/java/com/example/schedulingtasks/ScheduledTasksTest.java +++ b/src/test/java/com/example/schedulingtasks/ScheduledTasksTest.java @@ -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 @@ -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(); + }); + } } diff --git a/src/test/resources/application-config.yaml b/src/test/resources/application-config.yaml new file mode 100644 index 0000000..ecc959a --- /dev/null +++ b/src/test/resources/application-config.yaml @@ -0,0 +1,7 @@ +my: + app: + time: + report: + cycle: 3000 + delay: 1000 +# skip: true \ No newline at end of file