Add wire mock in order to stub endpoints to test suite
This commit is contained in:
parent
15abd8ef52
commit
e4a586b53d
|
@ -10,4 +10,7 @@
|
||||||
# Packaged files #
|
# Packaged files #
|
||||||
*.jar
|
*.jar
|
||||||
*.war
|
*.war
|
||||||
*.ear
|
*.ear
|
||||||
|
|
||||||
|
.externalToolBuilders
|
||||||
|
.settings
|
|
@ -7,48 +7,105 @@
|
||||||
|
|
||||||
<name>rest-assured</name>
|
<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 -->
|
<!-- utils -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.rest-assured</groupId>
|
<groupId>io.rest-assured</groupId>
|
||||||
<artifactId>rest-assured</artifactId>
|
<artifactId>rest-assured</artifactId>
|
||||||
<version>3.0.0</version>
|
<version>${rest-assured.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.rest-assured</groupId>
|
<groupId>io.rest-assured</groupId>
|
||||||
<artifactId>json-schema-validator</artifactId>
|
<artifactId>json-schema-validator</artifactId>
|
||||||
<version>3.0.0</version>
|
<version>${rest-assured.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.fge</groupId>
|
<groupId>com.github.fge</groupId>
|
||||||
<artifactId>json-schema-validator</artifactId>
|
<artifactId>json-schema-validator</artifactId>
|
||||||
<version>2.2.6</version>
|
<version>${json-schema-validator.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.fge</groupId>
|
<groupId>com.github.fge</groupId>
|
||||||
<artifactId>json-schema-core</artifactId>
|
<artifactId>json-schema-core</artifactId>
|
||||||
<version>1.2.5</version>
|
<version>${json-schema-core.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- web -->
|
|
||||||
|
|
||||||
<!-- test scoped -->
|
<!-- test scoped -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>com.github.tomakehurst</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>wiremock</artifactId>
|
||||||
<version>4.3</version>
|
<version>${wiremock.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.hamcrest</groupId>
|
<groupId>org.hamcrest</groupId>
|
||||||
<artifactId>hamcrest-all</artifactId>
|
<artifactId>hamcrest-all</artifactId>
|
||||||
<version>1.3</version>
|
<version>${hamcrest-all.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-core</artifactId>
|
||||||
|
<version>${mockito.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -1,20 +1,23 @@
|
||||||
package com.baeldung.restassured;
|
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.SchemaVersion;
|
||||||
import com.github.fge.jsonschema.cfg.ValidationConfiguration;
|
import com.github.fge.jsonschema.cfg.ValidationConfiguration;
|
||||||
import com.github.fge.jsonschema.main.JsonSchemaFactory;
|
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 {
|
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
|
@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()
|
get("/events?id=390").then().statusCode(200).assertThat()
|
||||||
.body("data.id", equalTo(390));
|
.body("id", equalTo(390));
|
||||||
|
|
||||||
|
wireMockServer.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUrl_whenJsonResponseHasArrayWithGivenValuesUnderKey_thenCorrect() {
|
public void givenUrl_whenJsonResponseHasArrayWithGivenValuesUnderKey_thenCorrect() {
|
||||||
|
|
||||||
get("/events?id=390").then().assertThat()
|
wireMockServer.start();
|
||||||
.body("odds.price", hasItems("1.30", "5.25"));
|
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() {
|
public void givenUrl_whenJsonResponseConformsToSchema_thenCorrect() {
|
||||||
get("/events?id=390").then().assertThat()
|
get("/events?id=390").then().assertThat()
|
||||||
.body(matchesJsonSchemaInClasspath("event_0.json"));
|
.body(matchesJsonSchemaInClasspath("event_0.json"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test @Ignore
|
||||||
public void givenUrl_whenValidatesResponseWithInstanceSettings_thenCorrect() {
|
public void givenUrl_whenValidatesResponseWithInstanceSettings_thenCorrect() {
|
||||||
JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory
|
JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory
|
||||||
.newBuilder()
|
.newBuilder()
|
||||||
|
@ -75,7 +130,7 @@ public class RestAssuredTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test @Ignore
|
||||||
public void givenUrl_whenValidatesResponseWithStaticSettings_thenCorrect() {
|
public void givenUrl_whenValidatesResponseWithStaticSettings_thenCorrect() {
|
||||||
|
|
||||||
get("/events?id=390")
|
get("/events?id=390")
|
||||||
|
@ -86,18 +141,18 @@ public class RestAssuredTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test @Ignore
|
||||||
public void givenUrl_whenCheckingFloatValuePasses_thenCorrect() {
|
public void givenUrl_whenCheckingFloatValuePasses_thenCorrect() {
|
||||||
get("/odd").then().assertThat().body("odd.ck", equalTo(12.2f));
|
get("/odd").then().assertThat().body("odd.ck", equalTo(12.2f));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test @Ignore
|
||||||
public void givenUrl_whenXmlResponseValueTestsEqual_thenCorrect() {
|
public void givenUrl_whenXmlResponseValueTestsEqual_thenCorrect() {
|
||||||
post("/employees").then().assertThat()
|
post("/employees").then().assertThat()
|
||||||
.body("employees.employee.first-name", equalTo("Jane"));
|
.body("employees.employee.first-name", equalTo("Jane"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test @Ignore
|
||||||
public void givenUrl_whenMultipleXmlValuesTestEqual_thenCorrect() {
|
public void givenUrl_whenMultipleXmlValuesTestEqual_thenCorrect() {
|
||||||
post("/employees").then().assertThat()
|
post("/employees").then().assertThat()
|
||||||
.body("employees.employee.first-name", equalTo("Jane"))
|
.body("employees.employee.first-name", equalTo("Jane"))
|
||||||
|
@ -105,7 +160,7 @@ public class RestAssuredTest {
|
||||||
.body("employees.employee.sex", equalTo("f"));
|
.body("employees.employee.sex", equalTo("f"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test @Ignore
|
||||||
public void givenUrl_whenMultipleXmlValuesTestEqualInShortHand_thenCorrect() {
|
public void givenUrl_whenMultipleXmlValuesTestEqualInShortHand_thenCorrect() {
|
||||||
post("/employees")
|
post("/employees")
|
||||||
.then()
|
.then()
|
||||||
|
@ -115,7 +170,7 @@ public class RestAssuredTest {
|
||||||
"employees.employee.sex", equalTo("f"));
|
"employees.employee.sex", equalTo("f"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test @Ignore
|
||||||
public void givenUrl_whenValidatesXmlUsingXpath_thenCorrect() {
|
public void givenUrl_whenValidatesXmlUsingXpath_thenCorrect() {
|
||||||
post("/employees")
|
post("/employees")
|
||||||
.then()
|
.then()
|
||||||
|
@ -125,7 +180,7 @@ public class RestAssuredTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test @Ignore
|
||||||
public void givenUrl_whenValidatesXmlUsingXpath2_thenCorrect() {
|
public void givenUrl_whenValidatesXmlUsingXpath2_thenCorrect() {
|
||||||
post("/employees")
|
post("/employees")
|
||||||
.then()
|
.then()
|
||||||
|
@ -134,7 +189,7 @@ public class RestAssuredTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test @Ignore
|
||||||
public void givenUrl_whenVerifiesScienceTeacherFromXml_thenCorrect() {
|
public void givenUrl_whenVerifiesScienceTeacherFromXml_thenCorrect() {
|
||||||
get("/teachers")
|
get("/teachers")
|
||||||
.then()
|
.then()
|
||||||
|
@ -142,7 +197,7 @@ public class RestAssuredTest {
|
||||||
hasItems("math", "physics"));
|
hasItems("math", "physics"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test @Ignore
|
||||||
public void givenUrl_whenVerifiesOddPricesAccuratelyByStatus_thenCorrect() {
|
public void givenUrl_whenVerifiesOddPricesAccuratelyByStatus_thenCorrect() {
|
||||||
get("/odds").then().body("odds.findAll { it.status > 0 }.price",
|
get("/odds").then().body("odds.findAll { it.status > 0 }.price",
|
||||||
hasItems(1.30f, 1.20f));
|
hasItems(1.30f, 1.20f));
|
||||||
|
|
Loading…
Reference in New Issue