From f7957e2fa8d22ee15772465fe1aa5ec527b7938d Mon Sep 17 00:00:00 2001 From: amit2103 Date: Sat, 31 Aug 2019 22:49:12 +0530 Subject: [PATCH] [BAEL-16767] - Fix the integrations tests in spring-boot-ops --- spring-boot-ops/pom.xml | 11 ++++++-- .../baeldung/properties/ConfProperties.java | 6 ++--- .../main/resources/external/conf.properties | 6 ++--- .../RestartApplicationIntegrationTest.java | 25 +++++++++++-------- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/spring-boot-ops/pom.xml b/spring-boot-ops/pom.xml index f36434b682..f578a24163 100644 --- a/spring-boot-ops/pom.xml +++ b/spring-boot-ops/pom.xml @@ -85,11 +85,17 @@ ${jquery.version} - + org.springframework.cloud spring-cloud-context ${springcloud.version} + + + org.apache.httpcomponents + httpclient + ${httpclient.version} + @@ -190,7 +196,8 @@ 2.2 18.0 3.1.7 - 2.0.2.RELEASE + 2.0.2.RELEASE + 4.5.8 diff --git a/spring-boot-ops/src/main/java/com/baeldung/properties/ConfProperties.java b/spring-boot-ops/src/main/java/com/baeldung/properties/ConfProperties.java index 0b6041bb06..c6413324f3 100644 --- a/spring-boot-ops/src/main/java/com/baeldung/properties/ConfProperties.java +++ b/spring-boot-ops/src/main/java/com/baeldung/properties/ConfProperties.java @@ -6,13 +6,13 @@ import org.springframework.stereotype.Component; @Component public class ConfProperties { - @Value("${url}") + @Value("${db.url}") private String url; - @Value("${username}") + @Value("${db.username}") private String username; - @Value("${password}") + @Value("${db.password}") private String password; public String getUrl() { diff --git a/spring-boot-ops/src/main/resources/external/conf.properties b/spring-boot-ops/src/main/resources/external/conf.properties index cfcd23dc76..944b31cc5c 100644 --- a/spring-boot-ops/src/main/resources/external/conf.properties +++ b/spring-boot-ops/src/main/resources/external/conf.properties @@ -1,4 +1,4 @@ -url=jdbc:postgresql://localhost:5432/ -username=admin -password=root +db.url=jdbc:postgresql://localhost:5432/ +db.username=admin +db.password=root spring.main.allow-bean-definition-overriding=true \ No newline at end of file diff --git a/spring-boot-ops/src/test/java/com/baeldung/restart/RestartApplicationIntegrationTest.java b/spring-boot-ops/src/test/java/com/baeldung/restart/RestartApplicationIntegrationTest.java index 14e80c3ac7..48c876de87 100644 --- a/spring-boot-ops/src/test/java/com/baeldung/restart/RestartApplicationIntegrationTest.java +++ b/spring-boot-ops/src/test/java/com/baeldung/restart/RestartApplicationIntegrationTest.java @@ -2,25 +2,24 @@ package com.baeldung.restart; import static org.junit.Assert.assertEquals; -import org.junit.After; -import org.junit.Before; +import java.io.IOException; +import java.net.ServerSocket; + import org.junit.Test; import org.springframework.boot.test.web.client.TestRestTemplate; -import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; -import java.time.Duration; - public class RestartApplicationIntegrationTest { private TestRestTemplate restTemplate = new TestRestTemplate(); @Test public void givenBootApp_whenRestart_thenOk() throws Exception { - Application.main(new String[0]); + Integer port = findRandomOpenPortOnAllLocalInterfaces(); + Application.main(new String[] { String.format("--server.port=%d", port) }); - ResponseEntity response = restTemplate.exchange("http://localhost:8080/restart", + ResponseEntity response = restTemplate.exchange(String.format("http://localhost:%d/restart", port), HttpMethod.POST, null, Object.class); assertEquals(200, response.getStatusCode().value()); @@ -28,12 +27,18 @@ public class RestartApplicationIntegrationTest { @Test public void givenBootApp_whenRestartUsingActuator_thenOk() throws Exception { - Application.main(new String[] { "--server.port=8090" }); + Integer port = findRandomOpenPortOnAllLocalInterfaces(); + Application.main(new String[] { String.format("--server.port=%d", port) }); - ResponseEntity response = restTemplate.exchange("http://localhost:8090/restartApp", + ResponseEntity response = restTemplate.exchange(String.format("http://localhost:%d/restartApp", port), HttpMethod.POST, null, Object.class); assertEquals(200, response.getStatusCode().value()); } - + + private Integer findRandomOpenPortOnAllLocalInterfaces() throws IOException { + try (ServerSocket socket = new ServerSocket(0);) { + return socket.getLocalPort(); + } + } } \ No newline at end of file