File tree Expand file tree Collapse file tree 5 files changed +84
-1
lines changed
test/java/com/baeldung/scheduled Expand file tree Collapse file tree 5 files changed +84
-1
lines changed Original file line number Diff line number Diff line change 8989 <groupId >org.junit.jupiter</groupId >
9090 <artifactId >junit-jupiter-api</artifactId >
9191 </dependency >
92+ <dependency >
93+ <groupId >org.awaitility</groupId >
94+ <artifactId >awaitility</artifactId >
95+ <version >${awaitility.version} </version >
96+ <scope >test</scope >
97+ </dependency >
9298 <!-- restdocs -->
9399 <dependency >
94100 <groupId >org.springframework.restdocs</groupId >
156162 <commons-collections4 .version>4.1</commons-collections4 .version>
157163 <snippetsDirectory >${project.build.directory}/generated-snippets</snippetsDirectory >
158164 <maven-surefire-plugin .version>2.21.0</maven-surefire-plugin .version>
159-
165+ < awaitility .version>3.1.6</ awaitility .version>
160166 </properties >
161167
162168</project >
Original file line number Diff line number Diff line change 1+ package com .baeldung .config ;
2+
3+ import org .springframework .context .annotation .ComponentScan ;
4+ import org .springframework .context .annotation .Configuration ;
5+ import org .springframework .scheduling .annotation .EnableScheduling ;
6+
7+ @ Configuration
8+ @ EnableScheduling
9+ @ ComponentScan ("com.baeldung.scheduled" )
10+ public class ScheduledConfig {
11+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .scheduled ;
2+
3+ import org .springframework .scheduling .annotation .Scheduled ;
4+ import org .springframework .stereotype .Component ;
5+
6+ import java .util .concurrent .atomic .AtomicInteger ;
7+
8+ @ Component
9+ public class Counter {
10+ private final AtomicInteger count = new AtomicInteger (0 );
11+
12+ @ Scheduled (fixedDelay = 5 )
13+ public void scheduled () {
14+ this .count .incrementAndGet ();
15+ }
16+
17+ public int getInvocationCount () {
18+ return this .count .get ();
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .scheduled ;
2+
3+ import com .baeldung .config .ScheduledConfig ;
4+ import org .awaitility .Duration ;
5+ import org .junit .jupiter .api .Test ;
6+ import org .springframework .boot .test .mock .mockito .SpyBean ;
7+ import org .springframework .test .context .junit .jupiter .SpringJUnitConfig ;
8+
9+ import static org .awaitility .Awaitility .await ;
10+ import static org .mockito .Mockito .atLeast ;
11+ import static org .mockito .Mockito .verify ;
12+
13+ @ SpringJUnitConfig (ScheduledConfig .class )
14+ public class ScheduledAwaitilityIntegrationTest {
15+
16+ @ SpyBean private Counter counter ;
17+
18+ @ Test
19+ public void whenWaitOneSecond_thenScheduledIsCalledAtLeastTenTimes () {
20+ await ()
21+ .atMost (Duration .ONE_SECOND )
22+ .untilAsserted (() -> verify (counter , atLeast (10 )).scheduled ());
23+ }
24+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .scheduled ;
2+
3+ import com .baeldung .config .ScheduledConfig ;
4+ import org .junit .jupiter .api .Test ;
5+ import org .springframework .beans .factory .annotation .Autowired ;
6+ import org .springframework .test .context .junit .jupiter .SpringJUnitConfig ;
7+
8+ import static org .assertj .core .api .Assertions .assertThat ;
9+
10+ @ SpringJUnitConfig (ScheduledConfig .class )
11+ public class ScheduledIntegrationTest {
12+
13+ @ Autowired Counter counter ;
14+
15+ @ Test
16+ public void givenSleepBy100ms_whenGetInvocationCount_thenIsGreaterThanZero () throws InterruptedException {
17+ Thread .sleep (100L );
18+
19+ assertThat (counter .getInvocationCount ()).isGreaterThan (0 );
20+ }
21+
22+ }
You can’t perform that action at this time.
0 commit comments