From 48dc1f0db001db7ce536843fb41f55518e2231cf Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Thu, 12 Mar 2020 20:28:11 +0100 Subject: [PATCH] JAVA-966: Use randomly generated and available port --- .../RestAssured2IntegrationTest.java | 9 ++++---- .../RestAssuredIntegrationTest.java | 9 ++++---- .../RestAssuredXML2IntegrationTest.java | 10 ++++---- .../RestAssuredXMLIntegrationTest.java | 9 ++++---- .../java/com/baeldung/restassured/Util.java | 23 +++++++++++++++++++ 5 files changed, 44 insertions(+), 16 deletions(-) diff --git a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssured2IntegrationTest.java b/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssured2IntegrationTest.java index 805d67271d..c579f1c260 100644 --- a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssured2IntegrationTest.java +++ b/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssured2IntegrationTest.java @@ -17,8 +17,7 @@ import static io.restassured.RestAssured.with; import static org.hamcrest.Matchers.hasItems; public class RestAssured2IntegrationTest { - private static final int PORT = 8084; - private static WireMockServer wireMockServer = new WireMockServer(PORT); + private static WireMockServer wireMockServer; private static final String EVENTS_PATH = "/odds"; private static final String APPLICATION_JSON = "application/json"; @@ -27,9 +26,11 @@ public class RestAssured2IntegrationTest { @BeforeClass public static void before() throws Exception { System.out.println("Setting up!"); + final int port = Util.getAvailablePort(); + wireMockServer = new WireMockServer(port); wireMockServer.start(); - configureFor("localhost", PORT); - RestAssured.port = PORT; + configureFor("localhost", port); + RestAssured.port = port; stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn( aResponse().withStatus(200) .withHeader("Content-Type", APPLICATION_JSON) diff --git a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssuredIntegrationTest.java b/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssuredIntegrationTest.java index 4ad3cea22c..e4279897e2 100644 --- a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssuredIntegrationTest.java +++ b/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssuredIntegrationTest.java @@ -21,8 +21,7 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasItems; public class RestAssuredIntegrationTest { - private static final int PORT = 8083; - private static WireMockServer wireMockServer = new WireMockServer(PORT); + private static WireMockServer wireMockServer; private static final String EVENTS_PATH = "/events?id=390"; private static final String APPLICATION_JSON = "application/json"; @@ -31,9 +30,11 @@ public class RestAssuredIntegrationTest { @BeforeClass public static void before() throws Exception { System.out.println("Setting up!"); + final int port = Util.getAvailablePort(); + wireMockServer = new WireMockServer(port); wireMockServer.start(); - RestAssured.port = PORT; - configureFor("localhost", PORT); + RestAssured.port = port; + configureFor("localhost", port); stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn( aResponse().withStatus(200) .withHeader("Content-Type", APPLICATION_JSON) diff --git a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssuredXML2IntegrationTest.java b/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssuredXML2IntegrationTest.java index 4a29d8832f..f71cce603c 100644 --- a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssuredXML2IntegrationTest.java +++ b/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssuredXML2IntegrationTest.java @@ -12,11 +12,11 @@ import static com.github.tomakehurst.wiremock.client.WireMock.get; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; import static io.restassured.RestAssured.get; +import static io.restassured.RestAssured.port; import static org.hamcrest.Matchers.hasItems; public class RestAssuredXML2IntegrationTest { - private static final int PORT = 8082; - private static WireMockServer wireMockServer = new WireMockServer(PORT); + private static WireMockServer wireMockServer; private static final String EVENTS_PATH = "/teachers"; private static final String APPLICATION_XML = "application/xml"; @@ -25,9 +25,11 @@ public class RestAssuredXML2IntegrationTest { @BeforeClass public static void before() throws Exception { System.out.println("Setting up!"); + final int port = Util.getAvailablePort(); + wireMockServer = new WireMockServer(port); wireMockServer.start(); - RestAssured.port = PORT; - configureFor("localhost", PORT); + RestAssured.port = port; + configureFor("localhost", port); stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn( aResponse().withStatus(200) .withHeader("Content-Type", APPLICATION_XML) diff --git a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssuredXMLIntegrationTest.java b/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssuredXMLIntegrationTest.java index f1a22831f4..082dace526 100644 --- a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssuredXMLIntegrationTest.java +++ b/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssuredXMLIntegrationTest.java @@ -17,8 +17,7 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.xml.HasXPath.hasXPath; public class RestAssuredXMLIntegrationTest { - private static final int PORT = 8081; - private static WireMockServer wireMockServer = new WireMockServer(PORT); + private static WireMockServer wireMockServer; private static final String EVENTS_PATH = "/employees"; private static final String APPLICATION_XML = "application/xml"; @@ -27,9 +26,11 @@ public class RestAssuredXMLIntegrationTest { @BeforeClass public static void before() throws Exception { System.out.println("Setting up!"); + final int port = Util.getAvailablePort(); + wireMockServer = new WireMockServer(port); wireMockServer.start(); - configureFor("localhost", PORT); - RestAssured.port = PORT; + configureFor("localhost", port); + RestAssured.port = port; stubFor(post(urlEqualTo(EVENTS_PATH)).willReturn( aResponse().withStatus(200) .withHeader("Content-Type", APPLICATION_XML) diff --git a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/Util.java b/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/Util.java index 02dec87927..70c595f562 100644 --- a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/Util.java +++ b/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/Util.java @@ -1,10 +1,15 @@ package com.baeldung.restassured; +import java.io.IOException; import java.io.InputStream; +import java.net.ServerSocket; +import java.util.Random; import java.util.Scanner; final class Util { + private static final int DEFAULT_PORT = 8069; + private Util() { } @@ -12,4 +17,22 @@ final class Util { Scanner s = new Scanner(is).useDelimiter("\\A"); return s.hasNext() ? s.next() : ""; } + + static int getAvailablePort() { + return new Random() + .ints(6000, 9000) + .filter(Util::isFree) + .findFirst() + .orElse(DEFAULT_PORT); + } + + + private static boolean isFree(int port) { + try { + new ServerSocket(port).close(); + return true; + } catch (IOException e) { + return false; + } + } }