[BAEL-7574] - Fixed integration tests for spring-rest-simple module

This commit is contained in:
mmchsusan 2018-07-12 13:22:05 -04:00 committed by amit2103
parent 6335e28a9e
commit 9b89244961
5 changed files with 41 additions and 29 deletions

View File

@ -52,6 +52,13 @@
<artifactId>opencsv</artifactId> <artifactId>opencsv</artifactId>
<version>${opencsv.version}</version> <version>${opencsv.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>${awaitility.version}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<properties> <properties>
@ -59,6 +66,7 @@
<spring.batch.version>4.0.0.RELEASE</spring.batch.version> <spring.batch.version>4.0.0.RELEASE</spring.batch.version>
<sqlite.version>3.15.1</sqlite.version> <sqlite.version>3.15.1</sqlite.version>
<opencsv.version>4.1</opencsv.version> <opencsv.version>4.1</opencsv.version>
<awaitility.version>3.1.1</awaitility.version>
</properties> </properties>
</project> </project>

View File

@ -6,7 +6,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import org.baeldung.batchscheduler.model.Book; import org.baeldung.batchscheduler.model.Book;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -45,7 +45,7 @@ public class SpringBatchScheduler {
private AtomicBoolean enabled = new AtomicBoolean(true); private AtomicBoolean enabled = new AtomicBoolean(true);
private Date currentLaunchDate; private AtomicInteger batchRunCounter = new AtomicInteger(0);
private final Map<Object, ScheduledFuture<?>> scheduledTasks = new IdentityHashMap<>(); private final Map<Object, ScheduledFuture<?>> scheduledTasks = new IdentityHashMap<>();
@ -60,18 +60,14 @@ public class SpringBatchScheduler {
Date date = new Date(); Date date = new Date();
logger.debug("scheduler starts at " + date); logger.debug("scheduler starts at " + date);
if (enabled.get()) { if (enabled.get()) {
currentLaunchDate = date; JobExecution jobExecution = jobLauncher().run(job(), new JobParametersBuilder().addDate("launchDate", date)
JobExecution jobExecution = jobLauncher().run(job(), new JobParametersBuilder().addDate("launchDate", currentLaunchDate)
.toJobParameters()); .toJobParameters());
batchRunCounter.incrementAndGet();
logger.debug("Batch job ends with status as " + jobExecution.getStatus()); logger.debug("Batch job ends with status as " + jobExecution.getStatus());
} }
logger.debug("scheduler ends "); logger.debug("scheduler ends ");
} }
public Date getCurrentLaunchDate() {
return currentLaunchDate;
}
public void stop() { public void stop() {
enabled.set(false); enabled.set(false);
} }
@ -169,4 +165,8 @@ public class SpringBatchScheduler {
}; };
} }
public AtomicInteger getBatchRunCounter() {
return batchRunCounter;
}
} }

View File

@ -1,60 +1,60 @@
package org.baeldung.batchscheduler; package org.baeldung.batchscheduler;
import java.util.Date;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor; import org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.awaitility.Awaitility.await;
import static java.util.concurrent.TimeUnit.*;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringBatchScheduler.class) @ContextConfiguration(classes = SpringBatchScheduler.class)
public class SpringBatchSchedulerIntegrationTest { public class SpringBatchSchedulerIntegrationTest {
private static final int TIMER = 3000;
@Autowired @Autowired
private ApplicationContext context; private ApplicationContext context;
@Test @Test
public void stopJobsWhenSchedulerDisabled() throws Exception { public void stopJobsWhenSchedulerDisabled() throws Exception {
Thread.sleep(TIMER);
SpringBatchScheduler schedulerBean = context.getBean(SpringBatchScheduler.class); SpringBatchScheduler schedulerBean = context.getBean(SpringBatchScheduler.class);
await().untilAsserted(() -> Assert.assertEquals(2, schedulerBean.getBatchRunCounter()
.get()));
schedulerBean.stop(); schedulerBean.stop();
Thread.sleep(TIMER); await().atLeast(3, SECONDS);
Date lastLaunchDate = schedulerBean.getCurrentLaunchDate();
Thread.sleep(TIMER); Assert.assertEquals(2, schedulerBean.getBatchRunCounter()
Assert.assertEquals(lastLaunchDate, schedulerBean.getCurrentLaunchDate()); .get());
} }
@Test @Test
public void stopJobSchedulerWhenSchedulerDestroyed() throws Exception { public void stopJobSchedulerWhenSchedulerDestroyed() throws Exception {
Thread.sleep(TIMER);
ScheduledAnnotationBeanPostProcessor bean = context.getBean(ScheduledAnnotationBeanPostProcessor.class); ScheduledAnnotationBeanPostProcessor bean = context.getBean(ScheduledAnnotationBeanPostProcessor.class);
SpringBatchScheduler schedulerBean = context.getBean(SpringBatchScheduler.class); SpringBatchScheduler schedulerBean = context.getBean(SpringBatchScheduler.class);
await().untilAsserted(() -> Assert.assertEquals(2, schedulerBean.getBatchRunCounter()
.get()));
bean.postProcessBeforeDestruction(schedulerBean, "SpringBatchScheduler"); bean.postProcessBeforeDestruction(schedulerBean, "SpringBatchScheduler");
Thread.sleep(TIMER); await().atLeast(3, SECONDS);
Date lastLaunchTime = schedulerBean.getCurrentLaunchDate();
Thread.sleep(TIMER);
Assert.assertEquals(lastLaunchTime, schedulerBean.getCurrentLaunchDate());
Assert.assertEquals(2, schedulerBean.getBatchRunCounter()
.get());
} }
@Test @Test
public void stopJobSchedulerWhenFutureTasksCancelled() throws Exception { public void stopJobSchedulerWhenFutureTasksCancelled() throws Exception {
Thread.sleep(TIMER);
SpringBatchScheduler schedulerBean = context.getBean(SpringBatchScheduler.class); SpringBatchScheduler schedulerBean = context.getBean(SpringBatchScheduler.class);
await().untilAsserted(() -> Assert.assertEquals(2, schedulerBean.getBatchRunCounter()
.get()));
schedulerBean.cancelFutureSchedulerTasks(); schedulerBean.cancelFutureSchedulerTasks();
Thread.sleep(TIMER); await().atLeast(3, SECONDS);
Date lastLaunchTime = schedulerBean.getCurrentLaunchDate();
Thread.sleep(TIMER);
Assert.assertEquals(lastLaunchTime, schedulerBean.getCurrentLaunchDate());
Assert.assertEquals(2, schedulerBean.getBatchRunCounter()
.get());
} }
} }

View File

@ -8,6 +8,7 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.client.AutoConfigureWebClient;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.context.web.WebAppConfiguration;
@ -18,6 +19,7 @@ import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = WebConfig.class) @ContextConfiguration(classes = WebConfig.class)
@WebAppConfiguration @WebAppConfiguration
@AutoConfigureWebClient
public class ExampleControllerIntegrationTest { public class ExampleControllerIntegrationTest {
private MockMvc mockMvc; private MockMvc mockMvc;

View File

@ -15,6 +15,7 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.client.AutoConfigureWebClient;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.context.web.WebAppConfiguration;
@ -26,6 +27,7 @@ import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = WebConfig.class) @ContextConfiguration(classes = WebConfig.class)
@WebAppConfiguration @WebAppConfiguration
@AutoConfigureWebClient
public class BazzNewMappingsExampleIntegrationTest { public class BazzNewMappingsExampleIntegrationTest {
private MockMvc mockMvc; private MockMvc mockMvc;