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

View File

@ -6,7 +6,7 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import org.baeldung.batchscheduler.model.Book;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -45,7 +45,7 @@ public class SpringBatchScheduler {
private AtomicBoolean enabled = new AtomicBoolean(true);
private Date currentLaunchDate;
private AtomicInteger batchRunCounter = new AtomicInteger(0);
private final Map<Object, ScheduledFuture<?>> scheduledTasks = new IdentityHashMap<>();
@ -60,18 +60,14 @@ public class SpringBatchScheduler {
Date date = new Date();
logger.debug("scheduler starts at " + date);
if (enabled.get()) {
currentLaunchDate = date;
JobExecution jobExecution = jobLauncher().run(job(), new JobParametersBuilder().addDate("launchDate", currentLaunchDate)
JobExecution jobExecution = jobLauncher().run(job(), new JobParametersBuilder().addDate("launchDate", date)
.toJobParameters());
batchRunCounter.incrementAndGet();
logger.debug("Batch job ends with status as " + jobExecution.getStatus());
}
logger.debug("scheduler ends ");
}
public Date getCurrentLaunchDate() {
return currentLaunchDate;
}
public void stop() {
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;
import java.util.Date;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.awaitility.Awaitility.await;
import static java.util.concurrent.TimeUnit.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringBatchScheduler.class)
public class SpringBatchSchedulerIntegrationTest {
private static final int TIMER = 3000;
@Autowired
private ApplicationContext context;
@Test
public void stopJobsWhenSchedulerDisabled() throws Exception {
Thread.sleep(TIMER);
SpringBatchScheduler schedulerBean = context.getBean(SpringBatchScheduler.class);
await().untilAsserted(() -> Assert.assertEquals(2, schedulerBean.getBatchRunCounter()
.get()));
schedulerBean.stop();
Thread.sleep(TIMER);
Date lastLaunchDate = schedulerBean.getCurrentLaunchDate();
Thread.sleep(TIMER);
Assert.assertEquals(lastLaunchDate, schedulerBean.getCurrentLaunchDate());
await().atLeast(3, SECONDS);
Assert.assertEquals(2, schedulerBean.getBatchRunCounter()
.get());
}
@Test
public void stopJobSchedulerWhenSchedulerDestroyed() throws Exception {
Thread.sleep(TIMER);
ScheduledAnnotationBeanPostProcessor bean = context.getBean(ScheduledAnnotationBeanPostProcessor.class);
SpringBatchScheduler schedulerBean = context.getBean(SpringBatchScheduler.class);
await().untilAsserted(() -> Assert.assertEquals(2, schedulerBean.getBatchRunCounter()
.get()));
bean.postProcessBeforeDestruction(schedulerBean, "SpringBatchScheduler");
Thread.sleep(TIMER);
Date lastLaunchTime = schedulerBean.getCurrentLaunchDate();
Thread.sleep(TIMER);
Assert.assertEquals(lastLaunchTime, schedulerBean.getCurrentLaunchDate());
await().atLeast(3, SECONDS);
Assert.assertEquals(2, schedulerBean.getBatchRunCounter()
.get());
}
@Test
public void stopJobSchedulerWhenFutureTasksCancelled() throws Exception {
Thread.sleep(TIMER);
SpringBatchScheduler schedulerBean = context.getBean(SpringBatchScheduler.class);
await().untilAsserted(() -> Assert.assertEquals(2, schedulerBean.getBatchRunCounter()
.get()));
schedulerBean.cancelFutureSchedulerTasks();
Thread.sleep(TIMER);
Date lastLaunchTime = schedulerBean.getCurrentLaunchDate();
Thread.sleep(TIMER);
Assert.assertEquals(lastLaunchTime, schedulerBean.getCurrentLaunchDate());
await().atLeast(3, SECONDS);
Assert.assertEquals(2, schedulerBean.getBatchRunCounter()
.get());
}
}

View File

@ -8,6 +8,7 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
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.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@ -18,6 +19,7 @@ import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = WebConfig.class)
@WebAppConfiguration
@AutoConfigureWebClient
public class ExampleControllerIntegrationTest {
private MockMvc mockMvc;

View File

@ -15,6 +15,7 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
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.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@ -26,6 +27,7 @@ import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = WebConfig.class)
@WebAppConfiguration
@AutoConfigureWebClient
public class BazzNewMappingsExampleIntegrationTest {
private MockMvc mockMvc;