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
@@ -7,6 +7,8 @@ import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.*;
@RunWith(PowerMockRunner.class)
@@ -23,7 +25,7 @@ public class PowerMockitoIntegrationTest {
when(collaborator.helloMethod()).thenReturn("Hello Baeldung!");
String welcome = collaborator.helloMethod();
Mockito.verify(collaborator).helloMethod();
verify(collaborator).helloMethod();
assertEquals("Hello Baeldung!", welcome);
}
@@ -42,7 +44,7 @@ public class PowerMockitoIntegrationTest {
assertEquals("Hello Baeldung!", firstWelcome);
assertEquals("Hello Baeldung!", secondWelcome);
verifyStatic(Mockito.times(2));
verifyStatic(times(2));
CollaboratorWithStaticMethods.firstMethod(Mockito.anyString());
verifyStatic(Mockito.never());
@@ -67,7 +69,7 @@ public class PowerMockitoIntegrationTest {
when(mock.finalMethod()).thenReturn("I am a final mock method.");
returnValue = mock.finalMethod();
Mockito.verify(mock).finalMethod();
verify(mock,times(3)).finalMethod();
assertEquals("I am a final mock method.", returnValue);
when(mock, "privateMethod").thenReturn("I am a private mock method.");
@@ -24,7 +24,7 @@ public class MockitoMockIntegrationTest {
}
@Rule
private ExpectedException thrown = ExpectedException.none();
public ExpectedException thrown = ExpectedException.none();
@Test
public void whenUsingSimpleMock_thenCorrect() {
@@ -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);
}