BAEL-2490 Code samples (#6211)

* Added initial samples

* Added initial samples

* Awaitility updated
This commit is contained in:
Juan Moreno 2019-02-10 01:54:51 -03:00 committed by KevinGilmore
parent c60e6f1561
commit 312f633d15
5 changed files with 84 additions and 1 deletions

View File

@ -89,6 +89,12 @@
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>${awaitility.version}</version>
<scope>test</scope>
</dependency>
<!-- restdocs -->
<dependency>
<groupId>org.springframework.restdocs</groupId>
@ -156,7 +162,7 @@
<commons-collections4.version>4.1</commons-collections4.version>
<snippetsDirectory>${project.build.directory}/generated-snippets</snippetsDirectory>
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
<awaitility.version>3.1.6</awaitility.version>
</properties>
</project>

View File

@ -0,0 +1,11 @@
package com.baeldung.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
@Configuration
@EnableScheduling
@ComponentScan("com.baeldung.scheduled")
public class ScheduledConfig {
}

View File

@ -0,0 +1,20 @@
package com.baeldung.scheduled;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.concurrent.atomic.AtomicInteger;
@Component
public class Counter {
private final AtomicInteger count = new AtomicInteger(0);
@Scheduled(fixedDelay = 5)
public void scheduled() {
this.count.incrementAndGet();
}
public int getInvocationCount() {
return this.count.get();
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.scheduled;
import com.baeldung.config.ScheduledConfig;
import org.awaitility.Duration;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.awaitility.Awaitility.await;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.verify;
@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());
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.scheduled;
import com.baeldung.config.ScheduledConfig;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@SpringJUnitConfig(ScheduledConfig.class)
public class ScheduledIntegrationTest {
@Autowired Counter counter;
@Test
public void givenSleepBy100ms_whenGetInvocationCount_thenIsGreaterThanZero() throws InterruptedException {
Thread.sleep(100L);
assertThat(counter.getInvocationCount()).isGreaterThan(0);
}
}