Skip to content

Commit 312f633

Browse files
earth001KevinGilmore
authored andcommitted
BAEL-2490 Code samples (eugenp#6211)
* Added initial samples * Added initial samples * Awaitility updated
1 parent c60e6f1 commit 312f633

File tree

5 files changed

+84
-1
lines changed

5 files changed

+84
-1
lines changed

spring-5/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@
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>
@@ -156,7 +162,7 @@
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>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

0 commit comments

Comments
 (0)