From 9b89244961d62cbf1ef9f5a84037fff89b0b9792 Mon Sep 17 00:00:00 2001 From: mmchsusan Date: Thu, 12 Jul 2018 13:22:05 -0400 Subject: [PATCH 1/4] [BAEL-7574] - Fixed integration tests for spring-rest-simple module --- spring-batch/pom.xml | 8 ++++ .../batchscheduler/SpringBatchScheduler.java | 16 +++---- .../SpringBatchSchedulerIntegrationTest.java | 42 +++++++++---------- .../ExampleControllerIntegrationTest.java | 2 + ...BazzNewMappingsExampleIntegrationTest.java | 2 + 5 files changed, 41 insertions(+), 29 deletions(-) diff --git a/spring-batch/pom.xml b/spring-batch/pom.xml index 9a7e9b7f57..cfd725b2bd 100644 --- a/spring-batch/pom.xml +++ b/spring-batch/pom.xml @@ -52,6 +52,13 @@ opencsv ${opencsv.version} + + + org.awaitility + awaitility + ${awaitility.version} + test + @@ -59,6 +66,7 @@ 4.0.0.RELEASE 3.15.1 4.1 + 3.1.1 diff --git a/spring-batch/src/main/java/org/baeldung/batchscheduler/SpringBatchScheduler.java b/spring-batch/src/main/java/org/baeldung/batchscheduler/SpringBatchScheduler.java index edb9b7cfa5..1beeb6b2bf 100644 --- a/spring-batch/src/main/java/org/baeldung/batchscheduler/SpringBatchScheduler.java +++ b/spring-batch/src/main/java/org/baeldung/batchscheduler/SpringBatchScheduler.java @@ -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> 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; + } + } diff --git a/spring-batch/src/test/java/org/baeldung/batchscheduler/SpringBatchSchedulerIntegrationTest.java b/spring-batch/src/test/java/org/baeldung/batchscheduler/SpringBatchSchedulerIntegrationTest.java index ef825b900f..8927421ac5 100644 --- a/spring-batch/src/test/java/org/baeldung/batchscheduler/SpringBatchSchedulerIntegrationTest.java +++ b/spring-batch/src/test/java/org/baeldung/batchscheduler/SpringBatchSchedulerIntegrationTest.java @@ -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()); } - + + } diff --git a/spring-rest-simple/src/test/java/org/baeldung/web/controller/status/ExampleControllerIntegrationTest.java b/spring-rest-simple/src/test/java/org/baeldung/web/controller/status/ExampleControllerIntegrationTest.java index e29bef501e..7f78146b84 100644 --- a/spring-rest-simple/src/test/java/org/baeldung/web/controller/status/ExampleControllerIntegrationTest.java +++ b/spring-rest-simple/src/test/java/org/baeldung/web/controller/status/ExampleControllerIntegrationTest.java @@ -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; diff --git a/spring-rest-simple/src/test/java/org/baeldung/web/test/BazzNewMappingsExampleIntegrationTest.java b/spring-rest-simple/src/test/java/org/baeldung/web/test/BazzNewMappingsExampleIntegrationTest.java index 266ee1938b..dfb3ff7a38 100644 --- a/spring-rest-simple/src/test/java/org/baeldung/web/test/BazzNewMappingsExampleIntegrationTest.java +++ b/spring-rest-simple/src/test/java/org/baeldung/web/test/BazzNewMappingsExampleIntegrationTest.java @@ -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; From a16ec5a9dde07868236002b711f31dfd032ce52b Mon Sep 17 00:00:00 2001 From: amit2103 Date: Fri, 13 Jul 2018 01:13:27 +0530 Subject: [PATCH 2/4] Fixed integration tests for spring-all, data-flow-server, spring-cloud-rest-books-api, spring-cloud-rest-reviews-api modules --- .../CacheRefinementsIntegrationTest.java | 2 +- .../data-flow-server/pom.xml | 8 ++-- ...aFlowServerApplicationIntegrationTest.java | 27 +++++++++++ .../org/baeldung/BooksApiIntegrationTest.java | 27 +++++++++++ .../BookReviewsApiIntegrationTest.java | 47 +++++++++++-------- 5 files changed, 86 insertions(+), 25 deletions(-) diff --git a/spring-all/src/test/java/org/baeldung/spring43/cache/CacheRefinementsIntegrationTest.java b/spring-all/src/test/java/org/baeldung/spring43/cache/CacheRefinementsIntegrationTest.java index 986932dafe..fa72ed6d43 100644 --- a/spring-all/src/test/java/org/baeldung/spring43/cache/CacheRefinementsIntegrationTest.java +++ b/spring-all/src/test/java/org/baeldung/spring43/cache/CacheRefinementsIntegrationTest.java @@ -25,7 +25,7 @@ public class CacheRefinementsIntegrationTest extends AbstractJUnit4SpringContext executorService.execute(() -> service.getFoo("test").printInstanceNumber()); } executorService.awaitTermination(1, TimeUnit.SECONDS); - assertEquals(Foo.getInstanceCount(), 1); + assertEquals(1, Foo.getInstanceCount()); } } diff --git a/spring-cloud-data-flow/data-flow-server/pom.xml b/spring-cloud-data-flow/data-flow-server/pom.xml index 0133d65978..3b04985618 100644 --- a/spring-cloud-data-flow/data-flow-server/pom.xml +++ b/spring-cloud-data-flow/data-flow-server/pom.xml @@ -11,10 +11,10 @@ Demo project for Spring Boot - parent-boot-1 - com.baeldung - 0.0.1-SNAPSHOT - ../../parent-boot-1 + org.springframework.boot + spring-boot-starter-parent + 1.4.3.RELEASE + diff --git a/spring-cloud-data-flow/data-flow-server/src/test/java/org/baeldung/spring/cloud/DataFlowServerApplicationIntegrationTest.java b/spring-cloud-data-flow/data-flow-server/src/test/java/org/baeldung/spring/cloud/DataFlowServerApplicationIntegrationTest.java index bb8660b816..9eba12fc12 100644 --- a/spring-cloud-data-flow/data-flow-server/src/test/java/org/baeldung/spring/cloud/DataFlowServerApplicationIntegrationTest.java +++ b/spring-cloud-data-flow/data-flow-server/src/test/java/org/baeldung/spring/cloud/DataFlowServerApplicationIntegrationTest.java @@ -2,7 +2,14 @@ package org.baeldung.spring.cloud; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mockito; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnection; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.serializer.RedisSerializer; +import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @@ -13,4 +20,24 @@ public class DataFlowServerApplicationIntegrationTest { public void contextLoads() { } + @EnableRedisHttpSession + @Configuration + static class Config { + + @Bean + @SuppressWarnings("unchecked") + public RedisSerializer defaultRedisSerializer() { + return Mockito.mock(RedisSerializer.class); + } + + @Bean + public RedisConnectionFactory connectionFactory() { + + RedisConnectionFactory factory = Mockito.mock(RedisConnectionFactory.class); + RedisConnection connection = Mockito.mock(RedisConnection.class); + Mockito.when(factory.getConnection()).thenReturn(connection); + + return factory; + } + } } diff --git a/spring-cloud/spring-cloud-rest/spring-cloud-rest-books-api/src/test/java/org/baeldung/BooksApiIntegrationTest.java b/spring-cloud/spring-cloud-rest/spring-cloud-rest-books-api/src/test/java/org/baeldung/BooksApiIntegrationTest.java index d6b0ee1694..15056e110c 100644 --- a/spring-cloud/spring-cloud-rest/spring-cloud-rest-books-api/src/test/java/org/baeldung/BooksApiIntegrationTest.java +++ b/spring-cloud/spring-cloud-rest/spring-cloud-rest-books-api/src/test/java/org/baeldung/BooksApiIntegrationTest.java @@ -2,7 +2,14 @@ package org.baeldung; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mockito; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnection; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.serializer.RedisSerializer; +import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @@ -13,4 +20,24 @@ public class BooksApiIntegrationTest { public void contextLoads() { } + @EnableRedisHttpSession + @Configuration + static class Config { + + @Bean + @SuppressWarnings("unchecked") + public RedisSerializer defaultRedisSerializer() { + return Mockito.mock(RedisSerializer.class); + } + + @Bean + public RedisConnectionFactory connectionFactory() { + + RedisConnectionFactory factory = Mockito.mock(RedisConnectionFactory.class); + RedisConnection connection = Mockito.mock(RedisConnection.class); + Mockito.when(factory.getConnection()).thenReturn(connection); + + return factory; + } + } } diff --git a/spring-cloud/spring-cloud-rest/spring-cloud-rest-reviews-api/src/test/java/org/baeldung/BookReviewsApiIntegrationTest.java b/spring-cloud/spring-cloud-rest/spring-cloud-rest-reviews-api/src/test/java/org/baeldung/BookReviewsApiIntegrationTest.java index 98cda102e9..c63b4ebb08 100644 --- a/spring-cloud/spring-cloud-rest/spring-cloud-rest-reviews-api/src/test/java/org/baeldung/BookReviewsApiIntegrationTest.java +++ b/spring-cloud/spring-cloud-rest/spring-cloud-rest-reviews-api/src/test/java/org/baeldung/BookReviewsApiIntegrationTest.java @@ -1,36 +1,43 @@ package org.baeldung; -import java.io.IOException; - -import org.junit.AfterClass; -import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mockito; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnection; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.serializer.RedisSerializer; +import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; import org.springframework.test.context.junit4.SpringRunner; -import redis.embedded.RedisServer; @RunWith(SpringRunner.class) @SpringBootTest public class BookReviewsApiIntegrationTest { - - private static RedisServer redisServer; - private static int port; - - @BeforeClass - public static void setUp() throws IOException { - - redisServer = new RedisServer(6379); - redisServer.start(); - } - - @AfterClass - public static void destroy() { - redisServer.stop(); - } @Test public void contextLoads() { } + @EnableRedisHttpSession + @Configuration + static class Config { + + @Bean + @SuppressWarnings("unchecked") + public RedisSerializer defaultRedisSerializer() { + return Mockito.mock(RedisSerializer.class); + } + + @Bean + public RedisConnectionFactory connectionFactory() { + + RedisConnectionFactory factory = Mockito.mock(RedisConnectionFactory.class); + RedisConnection connection = Mockito.mock(RedisConnection.class); + Mockito.when(factory.getConnection()).thenReturn(connection); + + return factory; + } + } } From fc722ec6d8de705289408117ebe2e7704eca9276 Mon Sep 17 00:00:00 2001 From: amit2103 Date: Fri, 13 Jul 2018 22:32:11 +0530 Subject: [PATCH 3/4] [BAEL-7614] - Upgraded spring-boot and spring-test version to fix integration test for spring-data-neo4j module --- persistence-modules/spring-data-neo4j/pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/persistence-modules/spring-data-neo4j/pom.xml b/persistence-modules/spring-data-neo4j/pom.xml index 26352df1ee..923d877fd7 100644 --- a/persistence-modules/spring-data-neo4j/pom.xml +++ b/persistence-modules/spring-data-neo4j/pom.xml @@ -110,8 +110,8 @@ 3.1.0 4.1.6.RELEASE 1.1 - 1.4.3.RELEASE - 4.3.5.RELEASE + 1.5.13.RELEASE + 4.3.17.RELEASE 2.1.1 From 86ae0483f3dbc30a035e96cca95f0f8a752e65da Mon Sep 17 00:00:00 2001 From: amit2103 Date: Fri, 13 Jul 2018 22:51:03 +0530 Subject: [PATCH 4/4] [BAEL-7614] - Renamed integration test to live test as live solr server endpoint is needed to fix spring-data-solr module --- ...itoryIntegrationTest.java => ProductRepositoryLiveTest.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename persistence-modules/spring-data-solr/src/test/java/com/baeldung/spring/data/solr/repo/{ProductRepositoryIntegrationTest.java => ProductRepositoryLiveTest.java} (98%) diff --git a/persistence-modules/spring-data-solr/src/test/java/com/baeldung/spring/data/solr/repo/ProductRepositoryIntegrationTest.java b/persistence-modules/spring-data-solr/src/test/java/com/baeldung/spring/data/solr/repo/ProductRepositoryLiveTest.java similarity index 98% rename from persistence-modules/spring-data-solr/src/test/java/com/baeldung/spring/data/solr/repo/ProductRepositoryIntegrationTest.java rename to persistence-modules/spring-data-solr/src/test/java/com/baeldung/spring/data/solr/repo/ProductRepositoryLiveTest.java index a3765a74ec..f86adcdc8a 100644 --- a/persistence-modules/spring-data-solr/src/test/java/com/baeldung/spring/data/solr/repo/ProductRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-solr/src/test/java/com/baeldung/spring/data/solr/repo/ProductRepositoryLiveTest.java @@ -20,7 +20,7 @@ import com.baeldung.spring.data.solr.repository.ProductRepository; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = SolrConfig.class) -public class ProductRepositoryIntegrationTest { +public class ProductRepositoryLiveTest { @Autowired private ProductRepository productRepository;