From e4a586b53de365bc925cc160385ff6320d6a0960 Mon Sep 17 00:00:00 2001 From: Alex Theedom Date: Mon, 18 Jul 2016 22:29:12 +0100 Subject: [PATCH] Add wire mock in order to stub endpoints to test suite --- rest-assured-tutorial/.gitignore | 5 +- rest-assured-tutorial/pom.xml | 83 ++++++++++--- .../baeldung/restassured/RestAssuredTest.java | 109 +++++++++++++----- 3 files changed, 156 insertions(+), 41 deletions(-) diff --git a/rest-assured-tutorial/.gitignore b/rest-assured-tutorial/.gitignore index 83c05e60c8..862f46031e 100644 --- a/rest-assured-tutorial/.gitignore +++ b/rest-assured-tutorial/.gitignore @@ -10,4 +10,7 @@ # Packaged files # *.jar *.war -*.ear \ No newline at end of file +*.ear + +.externalToolBuilders +.settings \ No newline at end of file diff --git a/rest-assured-tutorial/pom.xml b/rest-assured-tutorial/pom.xml index 12c1fe25ca..51b22fa0ff 100644 --- a/rest-assured-tutorial/pom.xml +++ b/rest-assured-tutorial/pom.xml @@ -7,48 +7,105 @@ rest-assured - + + + 3.5.1 + 2.19.1 + 1.10.19 + 4.12 + 2.1.7 + 1.3 + 1.2.5 + 2.2.6 + 3.0.0 + + + 1.7.13 + 1.1.3 + + + + + + + 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} + io.rest-assured rest-assured - 3.0.0 + ${rest-assured.version} test + io.rest-assured json-schema-validator - 3.0.0 + ${rest-assured.version} + com.github.fge json-schema-validator - 2.2.6 + ${json-schema-validator.version} + com.github.fge json-schema-core - 1.2.5 + ${json-schema-core.version} - + + junit + junit + ${junit.version} + test + - - junit - junit - 4.3 - test - + + com.github.tomakehurst + wiremock + ${wiremock.version} + test + org.hamcrest hamcrest-all - 1.3 + ${hamcrest-all.version} + test + + org.mockito + mockito-core + ${mockito.version} + test + + diff --git a/rest-assured-tutorial/src/test/java/com/baeldung/restassured/RestAssuredTest.java b/rest-assured-tutorial/src/test/java/com/baeldung/restassured/RestAssuredTest.java index 55c7a6f0d0..6ac2d91ee0 100644 --- a/rest-assured-tutorial/src/test/java/com/baeldung/restassured/RestAssuredTest.java +++ b/rest-assured-tutorial/src/test/java/com/baeldung/restassured/RestAssuredTest.java @@ -1,20 +1,23 @@ package com.baeldung.restassured; -import static io.restassured.RestAssured.get; -import static io.restassured.RestAssured.post; -import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath; -import static io.restassured.module.jsv.JsonSchemaValidatorSettings.settings; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.hasItems; -import static org.hamcrest.Matchers.containsString; -import io.restassured.module.jsv.JsonSchemaValidator; -import static org.hamcrest.xml.HasXPath.hasXPath; - -import org.junit.Test; - import com.github.fge.jsonschema.SchemaVersion; import com.github.fge.jsonschema.cfg.ValidationConfiguration; import com.github.fge.jsonschema.main.JsonSchemaFactory; +import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.client.WireMock; +import io.restassured.module.jsv.JsonSchemaValidator; +import org.junit.Ignore; +import org.junit.Test; + +import static com.github.tomakehurst.wiremock.client.WireMock.*; +import static io.restassured.RestAssured.get; +import static io.restassured.RestAssured.post; +import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath; +import static io.restassured.module.jsv.JsonSchemaValidatorSettings.settings; +import static org.hamcrest.Matchers.*; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.xml.HasXPath.hasXPath; + public class RestAssuredTest { @@ -36,29 +39,81 @@ public class RestAssuredTest { } + private WireMockServer wireMockServer = new WireMockServer(); + private static final String EVENTS_PATH = "/events?id=390"; + private static final String APPLICATION_JSON = "application/json"; + + private static final String GAME_ODDS = "" + + "{" + + " \"id\": 390," + + " \"data\": {" + + " \"countryId\": 35," + + " \"countryName\": \"Norway\"," + + " \"leagueName\": \"Norway 3\"," + + " \"status\": 0," + + " \"sportName\": \"Soccer\"," + + " \"time\": \"2016-06-12T12:00:00Z\"" + + " }," + + " \"odds\": [" + + " {" + + " \"price\": \"1.30\"," + + " \"status\": 0," + + " \"ck\": \"1\"," + + " \"name\": \"1\"" + + " }," + + " {" + + " \"price\": \"5.25\"," + + " \"status\": 0," + + " \"ck\": \"X\"," + + " \"name\": \"X\"" + + " }" + + " ]" + + "}"; + + @Test - public void givenUrl_whenSuccessOnGetsResponse_andJsonHasRequiredKV_thenCorrect() { + public void givenUrl_whenSuccessOnGetsResponse_andJsonHasRequiredKV_thenCorrect() { + + wireMockServer.start(); + configureFor("localhost", 8080); + + stubFor(WireMock.get(urlEqualTo(EVENTS_PATH)).willReturn(aResponse() + .withStatus(200) + .withHeader("Content-Type", APPLICATION_JSON) + .withBody(GAME_ODDS))); get("/events?id=390").then().statusCode(200).assertThat() - .body("data.id", equalTo(390)); + .body("id", equalTo(390)); + wireMockServer.stop(); } + @Test public void givenUrl_whenJsonResponseHasArrayWithGivenValuesUnderKey_thenCorrect() { - get("/events?id=390").then().assertThat() - .body("odds.price", hasItems("1.30", "5.25")); + wireMockServer.start(); + configureFor("localhost", 8080); + stubFor(WireMock.get(urlEqualTo(EVENTS_PATH)).willReturn(aResponse() + .withStatus(200) + .withHeader("Content-Type", APPLICATION_JSON) + .withBody(GAME_ODDS))); + + get("/events?id=390").then().assertThat() + .body("odds.price", hasItems("1.30", "5.25")); + + wireMockServer.stop(); } - @Test + + @Test @Ignore public void givenUrl_whenJsonResponseConformsToSchema_thenCorrect() { get("/events?id=390").then().assertThat() .body(matchesJsonSchemaInClasspath("event_0.json")); } - @Test + @Test @Ignore public void givenUrl_whenValidatesResponseWithInstanceSettings_thenCorrect() { JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory .newBuilder() @@ -75,7 +130,7 @@ public class RestAssuredTest { } - @Test + @Test @Ignore public void givenUrl_whenValidatesResponseWithStaticSettings_thenCorrect() { get("/events?id=390") @@ -86,18 +141,18 @@ public class RestAssuredTest { } - @Test + @Test @Ignore public void givenUrl_whenCheckingFloatValuePasses_thenCorrect() { get("/odd").then().assertThat().body("odd.ck", equalTo(12.2f)); } - @Test + @Test @Ignore public void givenUrl_whenXmlResponseValueTestsEqual_thenCorrect() { post("/employees").then().assertThat() .body("employees.employee.first-name", equalTo("Jane")); } - @Test + @Test @Ignore public void givenUrl_whenMultipleXmlValuesTestEqual_thenCorrect() { post("/employees").then().assertThat() .body("employees.employee.first-name", equalTo("Jane")) @@ -105,7 +160,7 @@ public class RestAssuredTest { .body("employees.employee.sex", equalTo("f")); } - @Test + @Test @Ignore public void givenUrl_whenMultipleXmlValuesTestEqualInShortHand_thenCorrect() { post("/employees") .then() @@ -115,7 +170,7 @@ public class RestAssuredTest { "employees.employee.sex", equalTo("f")); } - @Test + @Test @Ignore public void givenUrl_whenValidatesXmlUsingXpath_thenCorrect() { post("/employees") .then() @@ -125,7 +180,7 @@ public class RestAssuredTest { } - @Test + @Test @Ignore public void givenUrl_whenValidatesXmlUsingXpath2_thenCorrect() { post("/employees") .then() @@ -134,7 +189,7 @@ public class RestAssuredTest { } - @Test + @Test @Ignore public void givenUrl_whenVerifiesScienceTeacherFromXml_thenCorrect() { get("/teachers") .then() @@ -142,7 +197,7 @@ public class RestAssuredTest { hasItems("math", "physics")); } - @Test + @Test @Ignore public void givenUrl_whenVerifiesOddPricesAccuratelyByStatus_thenCorrect() { get("/odds").then().body("odds.findAll { it.status > 0 }.price", hasItems(1.30f, 1.20f));