Merge pull request #8871 from kwoyke/JAVA-966-fix
JAVA-966: Use randomly generated and available port
This commit is contained in:
commit
3714dd404f
|
@ -17,8 +17,7 @@ import static io.restassured.RestAssured.with;
|
||||||
import static org.hamcrest.Matchers.hasItems;
|
import static org.hamcrest.Matchers.hasItems;
|
||||||
|
|
||||||
public class RestAssured2IntegrationTest {
|
public class RestAssured2IntegrationTest {
|
||||||
private static final int PORT = 8084;
|
private static WireMockServer wireMockServer;
|
||||||
private static WireMockServer wireMockServer = new WireMockServer(PORT);
|
|
||||||
|
|
||||||
private static final String EVENTS_PATH = "/odds";
|
private static final String EVENTS_PATH = "/odds";
|
||||||
private static final String APPLICATION_JSON = "application/json";
|
private static final String APPLICATION_JSON = "application/json";
|
||||||
|
@ -27,9 +26,11 @@ public class RestAssured2IntegrationTest {
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void before() throws Exception {
|
public static void before() throws Exception {
|
||||||
System.out.println("Setting up!");
|
System.out.println("Setting up!");
|
||||||
|
final int port = Util.getAvailablePort();
|
||||||
|
wireMockServer = new WireMockServer(port);
|
||||||
wireMockServer.start();
|
wireMockServer.start();
|
||||||
configureFor("localhost", PORT);
|
configureFor("localhost", port);
|
||||||
RestAssured.port = PORT;
|
RestAssured.port = port;
|
||||||
stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn(
|
stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn(
|
||||||
aResponse().withStatus(200)
|
aResponse().withStatus(200)
|
||||||
.withHeader("Content-Type", APPLICATION_JSON)
|
.withHeader("Content-Type", APPLICATION_JSON)
|
||||||
|
|
|
@ -21,8 +21,7 @@ import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.hasItems;
|
import static org.hamcrest.Matchers.hasItems;
|
||||||
|
|
||||||
public class RestAssuredIntegrationTest {
|
public class RestAssuredIntegrationTest {
|
||||||
private static final int PORT = 8083;
|
private static WireMockServer wireMockServer;
|
||||||
private static WireMockServer wireMockServer = new WireMockServer(PORT);
|
|
||||||
|
|
||||||
private static final String EVENTS_PATH = "/events?id=390";
|
private static final String EVENTS_PATH = "/events?id=390";
|
||||||
private static final String APPLICATION_JSON = "application/json";
|
private static final String APPLICATION_JSON = "application/json";
|
||||||
|
@ -31,9 +30,11 @@ public class RestAssuredIntegrationTest {
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void before() throws Exception {
|
public static void before() throws Exception {
|
||||||
System.out.println("Setting up!");
|
System.out.println("Setting up!");
|
||||||
|
final int port = Util.getAvailablePort();
|
||||||
|
wireMockServer = new WireMockServer(port);
|
||||||
wireMockServer.start();
|
wireMockServer.start();
|
||||||
RestAssured.port = PORT;
|
RestAssured.port = port;
|
||||||
configureFor("localhost", PORT);
|
configureFor("localhost", port);
|
||||||
stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn(
|
stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn(
|
||||||
aResponse().withStatus(200)
|
aResponse().withStatus(200)
|
||||||
.withHeader("Content-Type", APPLICATION_JSON)
|
.withHeader("Content-Type", APPLICATION_JSON)
|
||||||
|
|
|
@ -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.stubFor;
|
||||||
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
|
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
|
||||||
import static io.restassured.RestAssured.get;
|
import static io.restassured.RestAssured.get;
|
||||||
|
import static io.restassured.RestAssured.port;
|
||||||
import static org.hamcrest.Matchers.hasItems;
|
import static org.hamcrest.Matchers.hasItems;
|
||||||
|
|
||||||
public class RestAssuredXML2IntegrationTest {
|
public class RestAssuredXML2IntegrationTest {
|
||||||
private static final int PORT = 8082;
|
private static WireMockServer wireMockServer;
|
||||||
private static WireMockServer wireMockServer = new WireMockServer(PORT);
|
|
||||||
|
|
||||||
private static final String EVENTS_PATH = "/teachers";
|
private static final String EVENTS_PATH = "/teachers";
|
||||||
private static final String APPLICATION_XML = "application/xml";
|
private static final String APPLICATION_XML = "application/xml";
|
||||||
|
@ -25,9 +25,11 @@ public class RestAssuredXML2IntegrationTest {
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void before() throws Exception {
|
public static void before() throws Exception {
|
||||||
System.out.println("Setting up!");
|
System.out.println("Setting up!");
|
||||||
|
final int port = Util.getAvailablePort();
|
||||||
|
wireMockServer = new WireMockServer(port);
|
||||||
wireMockServer.start();
|
wireMockServer.start();
|
||||||
RestAssured.port = PORT;
|
RestAssured.port = port;
|
||||||
configureFor("localhost", PORT);
|
configureFor("localhost", port);
|
||||||
stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn(
|
stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn(
|
||||||
aResponse().withStatus(200)
|
aResponse().withStatus(200)
|
||||||
.withHeader("Content-Type", APPLICATION_XML)
|
.withHeader("Content-Type", APPLICATION_XML)
|
||||||
|
|
|
@ -17,8 +17,7 @@ import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.xml.HasXPath.hasXPath;
|
import static org.hamcrest.xml.HasXPath.hasXPath;
|
||||||
|
|
||||||
public class RestAssuredXMLIntegrationTest {
|
public class RestAssuredXMLIntegrationTest {
|
||||||
private static final int PORT = 8081;
|
private static WireMockServer wireMockServer;
|
||||||
private static WireMockServer wireMockServer = new WireMockServer(PORT);
|
|
||||||
|
|
||||||
private static final String EVENTS_PATH = "/employees";
|
private static final String EVENTS_PATH = "/employees";
|
||||||
private static final String APPLICATION_XML = "application/xml";
|
private static final String APPLICATION_XML = "application/xml";
|
||||||
|
@ -27,9 +26,11 @@ public class RestAssuredXMLIntegrationTest {
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void before() throws Exception {
|
public static void before() throws Exception {
|
||||||
System.out.println("Setting up!");
|
System.out.println("Setting up!");
|
||||||
|
final int port = Util.getAvailablePort();
|
||||||
|
wireMockServer = new WireMockServer(port);
|
||||||
wireMockServer.start();
|
wireMockServer.start();
|
||||||
configureFor("localhost", PORT);
|
configureFor("localhost", port);
|
||||||
RestAssured.port = PORT;
|
RestAssured.port = port;
|
||||||
stubFor(post(urlEqualTo(EVENTS_PATH)).willReturn(
|
stubFor(post(urlEqualTo(EVENTS_PATH)).willReturn(
|
||||||
aResponse().withStatus(200)
|
aResponse().withStatus(200)
|
||||||
.withHeader("Content-Type", APPLICATION_XML)
|
.withHeader("Content-Type", APPLICATION_XML)
|
||||||
|
|
|
@ -1,10 +1,15 @@
|
||||||
package com.baeldung.restassured;
|
package com.baeldung.restassured;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.util.Random;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
final class Util {
|
final class Util {
|
||||||
|
|
||||||
|
private static final int DEFAULT_PORT = 8069;
|
||||||
|
|
||||||
private Util() {
|
private Util() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,4 +17,22 @@ final class Util {
|
||||||
Scanner s = new Scanner(is).useDelimiter("\\A");
|
Scanner s = new Scanner(is).useDelimiter("\\A");
|
||||||
return s.hasNext() ? s.next() : "";
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue