Add wire mock in order to stub endpoints to test suite

This commit is contained in:
Alex Theedom 2016-07-18 22:29:12 +01:00
parent 15abd8ef52
commit e4a586b53d
3 changed files with 156 additions and 41 deletions

View File

@ -10,4 +10,7 @@
# Packaged files #
*.jar
*.war
*.ear
*.ear
.externalToolBuilders
.settings

View File

@ -7,48 +7,105 @@
<name>rest-assured</name>
<dependencies>
<properties>
<!-- maven plugins -->
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
<mockito.version>1.10.19</mockito.version>
<junit.version>4.12</junit.version>
<wiremock.version>2.1.7</wiremock.version>
<hamcrest-all.version>1.3</hamcrest-all.version>
<json-schema-core.version>1.2.5</json-schema-core.version>
<json-schema-validator.version>2.2.6</json-schema-validator.version>
<rest-assured.version>3.0.0</rest-assured.version>
<!-- logging -->
<org.slf4j.version>1.7.13</org.slf4j.version>
<logback.version>1.1.3</logback.version>
</properties>
<dependencies>
<!-- logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<!-- <scope>runtime</scope> -->
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<!-- utils -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>3.0.0</version>
<version>${rest-assured.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>3.0.0</version>
<version>${rest-assured.version}</version>
</dependency>
<dependency>
<groupId>com.github.fge</groupId>
<artifactId>json-schema-validator</artifactId>
<version>2.2.6</version>
<version>${json-schema-validator.version}</version>
</dependency>
<dependency>
<groupId>com.github.fge</groupId>
<artifactId>json-schema-core</artifactId>
<version>1.2.5</version>
<version>${json-schema-core.version}</version>
</dependency>
<!-- web -->
<!-- test scoped -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>${wiremock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<version>${hamcrest-all.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -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));