JAVA-966: Use randomly generated and available port

This commit is contained in:
Krzysiek 2020-03-12 20:28:11 +01:00
parent 5fbc94ae90
commit 48dc1f0db0
5 changed files with 44 additions and 16 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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;
}
}
}