diff --git a/rest-testing/pom.xml b/rest-testing/pom.xml index e159af0b77..3bd7efb4d5 100644 --- a/rest-testing/pom.xml +++ b/rest-testing/pom.xml @@ -1,173 +1,180 @@ - - 4.0.0 - com.baeldung - rest-testing - 0.1-SNAPSHOT + + 4.0.0 + com.baeldung + rest-testing + 0.1-SNAPSHOT - rest-testing + rest-testing - + - + - - com.google.guava - guava - ${guava.version} - + + com.google.guava + guava + ${guava.version} + - - commons-io - commons-io - 2.4 - + + commons-io + commons-io + 2.4 + - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + - + - - org.apache.httpcomponents - httpclient - ${httpclient.version} - - - org.apache.httpcomponents - httpcore - ${httpcore.version} - + + org.apache.httpcomponents + httpclient + ${httpclient.version} + + + org.apache.httpcomponents + httpcore + ${httpcore.version} + - + - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + - + - - org.slf4j - slf4j-api - ${org.slf4j.version} - - - ch.qos.logback - logback-classic - ${logback.version} - - - - org.slf4j - jcl-over-slf4j - ${org.slf4j.version} - runtime - - - org.slf4j - log4j-over-slf4j - ${org.slf4j.version} - + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + + org.slf4j + jcl-over-slf4j + ${org.slf4j.version} + runtime + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + - + - - junit - junit - ${junit.version} - test - + + junit + junit + ${junit.version} + test + - - org.hamcrest - hamcrest-core - ${org.hamcrest.version} - test - - - org.hamcrest - hamcrest-library - ${org.hamcrest.version} - test - + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + - - org.mockito - mockito-core - ${mockito.version} - test - + + org.mockito + mockito-core + ${mockito.version} + test + + + com.github.tomakehurst + wiremock + 1.58 + test + - + - - rest-testing - - - src/main/resources - true - - + + rest-testing + + + src/main/resources + true + + - + - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 1.8 - 1.8 - - + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + - + - + - - - 2.7.2 + + + 2.7.2 - - 1.7.13 - 1.1.3 + + 1.7.13 + 1.1.3 - - 5.1.3.Final + + 5.1.3.Final - - 19.0 - 3.4 + + 19.0 + 3.4 - - 1.3 - 4.12 - 1.10.19 + + 1.3 + 4.12 + 1.10.19 - 4.4.1 - 4.5 + 4.4.1 + 4.5 - 2.9.0 + 2.9.0 - - 3.5.1 - 2.6 - 2.19.1 - 2.7 - 1.4.18 + + 3.5.1 + 2.6 + 2.19.1 + 2.7 + 1.4.18 - + \ No newline at end of file diff --git a/rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/JUnitManaged.java b/rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/JUnitManaged.java new file mode 100644 index 0000000000..9fef6017c1 --- /dev/null +++ b/rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/JUnitManaged.java @@ -0,0 +1,53 @@ +package com.baeldung.rest.wiremock.introduction; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.configureFor; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; +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.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.util.Scanner; + +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpGet; +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 JUnitManaged { + @Rule + public WireMockRule wireMockRule = new WireMockRule(); + + @Test + public void givenJUnitManagedServer_whenStubbingURL_thenCorrect() throws IOException { + configureFor("localhost", 8080); + stubFor(get(urlPathMatching("/baeldung/.*")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody("\"testing-library\": \"WireMock\""))); + + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpGet request = new HttpGet("http://localhost:8080/baeldung/wiremock"); + HttpResponse httpResponse = httpClient.execute(request); + String stringResponse = convertResponseToString(httpResponse); + + verify(getRequestedFor(urlEqualTo("/baeldung/wiremock"))); + assertEquals(200, httpResponse.getStatusLine().getStatusCode()); + assertEquals("application/json", httpResponse.getFirstHeader("Content-Type").getValue()); + assertEquals("\"testing-library\": \"WireMock\"", stringResponse); + } + + private String convertResponseToString(HttpResponse response) throws IOException { + InputStream responseStream = response.getEntity().getContent(); + Scanner scanner = new Scanner(responseStream, "UTF-8"); + String stringResponse = scanner.useDelimiter("\\Z").next(); + scanner.close(); + return stringResponse; + } +} diff --git a/rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/ProgrammaticallyManaged.java b/rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/ProgrammaticallyManaged.java new file mode 100644 index 0000000000..34b445ac0f --- /dev/null +++ b/rest-testing/src/test/java/com/baeldung/rest/wiremock/introduction/ProgrammaticallyManaged.java @@ -0,0 +1,52 @@ +package com.baeldung.rest.wiremock.introduction; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.configureFor; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; +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.verify; +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Scanner; + +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.junit.Test; + +import com.github.tomakehurst.wiremock.WireMockServer; + +public class ProgrammaticallyManaged { + WireMockServer wireMockServer = new WireMockServer(); + CloseableHttpClient httpClient = HttpClients.createDefault(); + + @Test + public void givenProgrammaticallyManagedServer_whenUsingSimpleStubbing_thenCorrect() throws IOException { + wireMockServer.start(); + + configureFor("localhost", 8080); + stubFor(get(urlEqualTo("/baeldung")).willReturn(aResponse().withBody("Welcome to Baeldung!"))); + + HttpGet request = new HttpGet("http://localhost:8080/baeldung"); + HttpResponse httpResponse = httpClient.execute(request); + String stringResponse = convertResponseToString(httpResponse); + + verify(getRequestedFor(urlEqualTo("/baeldung"))); + assertEquals("Welcome to Baeldung!", stringResponse); + + wireMockServer.stop(); + } + + private String convertResponseToString(HttpResponse response) throws IOException { + InputStream responseStream = response.getEntity().getContent(); + Scanner scanner = new Scanner(responseStream, "UTF-8"); + String stringResponse = scanner.useDelimiter("\\Z").next(); + scanner.close(); + return stringResponse; + } +} \ No newline at end of file