Bael 4461 2 (#4444)

* [BAEL-4462] - Fixed integration tests

* [BAEL-4462] - Fixed integration tests

* [BAEL-4462] - Fixed integration tests

* [BAEL-4462] - Fixed integration tests

* [BAEL-4462] - Fixed integration tests

* [BAEL-4462] - Fixed integration tests

* [BAEL-4462] - Fixed integration tests

* [BAEL-4462] - Fixed integration tests

* [BAEL-4462] - Fixed integration tests

* [BAEL-4462] - Fixed integration tests

* [BAEL-4462] - Fixed integration tests

* [BAEL-4462] - Fixed integration tests

* [BAEL-4462] - Fixed integration tests

* [BAEL-4462] - Fixed integration tests

* Fix verification times
This commit is contained in:
Amit Pandey
2018-06-11 13:48:30 +05:30
committed by Grzegorz Piwowarek
parent 096826cc07
commit a54c9e0c9e
40 changed files with 201 additions and 101 deletions

View File

@@ -1,19 +1,5 @@
package com.baeldung.rest.wiremock.introduction;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.junit.Rule;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.containing;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
@@ -29,16 +15,46 @@ import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.util.Scanner;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.junit.Rule;
import org.junit.Test;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
public class JUnitManagedIntegrationTest {
private static final String BAELDUNG_WIREMOCK_PATH = "/baeldung/wiremock";
private static final String APPLICATION_JSON = "application/json";
static int port;
static {
try {
// Get a free port
ServerSocket s = new ServerSocket(0);
port = s.getLocalPort();
s.close();
} catch (IOException e) {
// No OPS
}
}
@Rule
public WireMockRule wireMockRule = new WireMockRule();
public WireMockRule wireMockRule = new WireMockRule(port);
@Test
public void givenJUnitManagedServer_whenMatchingURL_thenCorrect() throws IOException {
stubFor(get(urlPathMatching("/baeldung/.*"))
.willReturn(aResponse()
.withStatus(200)
@@ -46,7 +62,7 @@ public class JUnitManagedIntegrationTest {
.withBody("\"testing-library\": \"WireMock\"")));
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet request = new HttpGet("http://localhost:8080/baeldung/wiremock");
HttpGet request = new HttpGet(String.format("http://localhost:%s/baeldung/wiremock", port));
HttpResponse httpResponse = httpClient.execute(request);
String stringResponse = convertHttpResponseToString(httpResponse);
@@ -66,7 +82,7 @@ public class JUnitManagedIntegrationTest {
.withBody("!!! Service Unavailable !!!")));
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet request = new HttpGet("http://localhost:8080/baeldung/wiremock");
HttpGet request = new HttpGet(String.format("http://localhost:%s/baeldung/wiremock", port));
request.addHeader("Accept", "text/html");
HttpResponse httpResponse = httpClient.execute(request);
String stringResponse = convertHttpResponseToString(httpResponse);
@@ -91,7 +107,7 @@ public class JUnitManagedIntegrationTest {
StringEntity entity = new StringEntity(jsonString);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost request = new HttpPost("http://localhost:8080/baeldung/wiremock");
HttpPost request = new HttpPost(String.format("http://localhost:%s/baeldung/wiremock", port));
request.addHeader("Content-Type", APPLICATION_JSON);
request.setEntity(entity);
HttpResponse response = httpClient.execute(request);
@@ -137,7 +153,7 @@ public class JUnitManagedIntegrationTest {
private HttpResponse generateClientAndReceiveResponseForPriorityTests() throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet request = new HttpGet("http://localhost:8080/baeldung/wiremock");
HttpGet request = new HttpGet(String.format("http://localhost:%s/baeldung/wiremock", port));
request.addHeader("Accept", "text/xml");
return httpClient.execute(request);
}