From 70ce72124bbc22c0396aaabe6c32f0d4e2e65a38 Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Tue, 17 Dec 2019 20:24:33 +0100 Subject: [PATCH] BAEL-3242: Use random port for test execution in spring-vertx module --- .../vertxspring/config/PortConfiguration.java | 32 +++++++++++++++++++ .../vertxspring/verticles/ServerVerticle.java | 6 +++- ...VertxSpringApplicationIntegrationTest.java | 14 +++++--- 3 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 spring-vertx/src/main/java/com/baeldung/vertxspring/config/PortConfiguration.java diff --git a/spring-vertx/src/main/java/com/baeldung/vertxspring/config/PortConfiguration.java b/spring-vertx/src/main/java/com/baeldung/vertxspring/config/PortConfiguration.java new file mode 100644 index 0000000000..50beb6c549 --- /dev/null +++ b/spring-vertx/src/main/java/com/baeldung/vertxspring/config/PortConfiguration.java @@ -0,0 +1,32 @@ +package com.baeldung.vertxspring.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; + +import java.io.IOException; +import java.net.ServerSocket; + +@Configuration +public class PortConfiguration { + + private static final int DEFAULT_PORT = 8069; + + + @Profile("default") + @Bean + public Integer defaultPort() { + return DEFAULT_PORT; + } + + @Profile("test") + @Bean + public Integer randomPort() { + try (ServerSocket socket = new ServerSocket(0)) { + return socket.getLocalPort(); + + } catch (IOException e) { + return DEFAULT_PORT; + } + } +} diff --git a/spring-vertx/src/main/java/com/baeldung/vertxspring/verticles/ServerVerticle.java b/spring-vertx/src/main/java/com/baeldung/vertxspring/verticles/ServerVerticle.java index 3c23cb15bc..a84845f030 100644 --- a/spring-vertx/src/main/java/com/baeldung/vertxspring/verticles/ServerVerticle.java +++ b/spring-vertx/src/main/java/com/baeldung/vertxspring/verticles/ServerVerticle.java @@ -1,5 +1,6 @@ package com.baeldung.vertxspring.verticles; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import io.vertx.core.AbstractVerticle; @@ -9,6 +10,9 @@ import io.vertx.ext.web.RoutingContext; @Component public class ServerVerticle extends AbstractVerticle { + @Autowired + private Integer defaultPort; + private void getAllArticlesHandler(RoutingContext routingContext) { vertx.eventBus() .send(ArticleRecipientVerticle.GET_ALL_ARTICLES, "", result -> { @@ -36,7 +40,7 @@ public class ServerVerticle extends AbstractVerticle { vertx.createHttpServer() .requestHandler(router::accept) - .listen(config().getInteger("http.port", 8080)); + .listen(config().getInteger("http.port", defaultPort)); } } diff --git a/spring-vertx/src/test/java/com/baeldung/vertxspring/VertxSpringApplicationIntegrationTest.java b/spring-vertx/src/test/java/com/baeldung/vertxspring/VertxSpringApplicationIntegrationTest.java index bfb902e5d4..1eda45eb3b 100644 --- a/spring-vertx/src/test/java/com/baeldung/vertxspring/VertxSpringApplicationIntegrationTest.java +++ b/spring-vertx/src/test/java/com/baeldung/vertxspring/VertxSpringApplicationIntegrationTest.java @@ -1,27 +1,33 @@ package com.baeldung.vertxspring; -import static org.junit.Assert.assertEquals; - import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import static org.junit.Assert.assertEquals; + @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest +@ActiveProfiles("test") public class VertxSpringApplicationIntegrationTest { + @Autowired + private Integer port; + private TestRestTemplate restTemplate = new TestRestTemplate(); @Test public void givenUrl_whenReceivedArticles_thenSuccess() throws InterruptedException { - ResponseEntity responseEntity = restTemplate.getForEntity("http://localhost:8080/api/baeldung/articles", String.class); + ResponseEntity responseEntity = restTemplate + .getForEntity("http://localhost:" + port + "/api/baeldung/articles", String.class); assertEquals(200, responseEntity.getStatusCodeValue()); } - }