Rest-assured fix (#2759)
This commit is contained in:
parent
e6d82e8ced
commit
fc0ed8fe3d
|
@ -1,55 +1,51 @@
|
||||||
package com.baeldung.restassured;
|
package com.baeldung.restassured;
|
||||||
|
|
||||||
|
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||||
|
import org.junit.AfterClass;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
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.configureFor;
|
||||||
import static com.github.tomakehurst.wiremock.client.WireMock.get;
|
import static com.github.tomakehurst.wiremock.client.WireMock.get;
|
||||||
import static com.github.tomakehurst.wiremock.client.WireMock.post;
|
|
||||||
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
|
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.urlEqualTo;
|
||||||
|
import static io.restassured.RestAssured.get;
|
||||||
import static org.hamcrest.Matchers.hasItems;
|
import static org.hamcrest.Matchers.hasItems;
|
||||||
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
import static io.restassured.RestAssured.get;
|
|
||||||
|
|
||||||
import com.github.tomakehurst.wiremock.WireMockServer;
|
|
||||||
|
|
||||||
public class RestAssured2IntegrationTest {
|
public class RestAssured2IntegrationTest {
|
||||||
private WireMockServer wireMockServer = new WireMockServer();
|
private static WireMockServer wireMockServer = new WireMockServer();
|
||||||
|
|
||||||
private static final String EVENTS_PATH = "/odds";
|
private static final String EVENTS_PATH = "/odds";
|
||||||
private static final String APPLICATION_JSON = "application/json";
|
private static final String APPLICATION_JSON = "application/json";
|
||||||
private static final String ODDS = getJson();
|
private static final String ODDS = getJson();
|
||||||
|
|
||||||
@Before
|
@BeforeClass
|
||||||
public void before() throws Exception {
|
public static void before() throws Exception {
|
||||||
System.out.println("Setting up!");
|
System.out.println("Setting up!");
|
||||||
wireMockServer.start();
|
wireMockServer.start();
|
||||||
configureFor("localhost", 8080);
|
configureFor("localhost", 8080);
|
||||||
stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn(
|
stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn(
|
||||||
aResponse().withStatus(200)
|
aResponse().withStatus(200)
|
||||||
.withHeader("Content-Type", APPLICATION_JSON)
|
.withHeader("Content-Type", APPLICATION_JSON)
|
||||||
.withBody(ODDS)));
|
.withBody(ODDS)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
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(5.25f, 1.2f));
|
hasItems(5.25f, 1.2f));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getJson() {
|
private static String getJson() {
|
||||||
|
|
||||||
return Util.inputStreamToString(new RestAssured2IntegrationTest().getClass()
|
return Util.inputStreamToString(RestAssured2IntegrationTest.class
|
||||||
.getResourceAsStream("/odds.json"));
|
.getResourceAsStream("/odds.json"));
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@After
|
|
||||||
public void after() throws Exception {
|
|
||||||
System.out.println("Running: tearDown");
|
|
||||||
wireMockServer.stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@AfterClass
|
||||||
|
public static void after() throws Exception {
|
||||||
|
System.out.println("Running: tearDown");
|
||||||
|
wireMockServer.stop();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,13 @@
|
||||||
package com.baeldung.restassured;
|
package com.baeldung.restassured;
|
||||||
|
|
||||||
|
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 org.junit.AfterClass;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
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.configureFor;
|
||||||
import static com.github.tomakehurst.wiremock.client.WireMock.get;
|
import static com.github.tomakehurst.wiremock.client.WireMock.get;
|
||||||
|
@ -11,96 +19,84 @@ import static io.restassured.module.jsv.JsonSchemaValidatorSettings.settings;
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.hasItems;
|
import static org.hamcrest.Matchers.hasItems;
|
||||||
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Before;
|
|
||||||
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;
|
|
||||||
|
|
||||||
public class RestAssuredIntegrationTest {
|
public class RestAssuredIntegrationTest {
|
||||||
|
|
||||||
private WireMockServer wireMockServer = new WireMockServer();
|
private static WireMockServer wireMockServer = new WireMockServer();
|
||||||
private static final String EVENTS_PATH = "/events?id=390";
|
private static final String EVENTS_PATH = "/events?id=390";
|
||||||
private static final String APPLICATION_JSON = "application/json";
|
private static final String APPLICATION_JSON = "application/json";
|
||||||
private static final String GAME_ODDS = getEventJson();
|
private static final String GAME_ODDS = getEventJson();
|
||||||
|
|
||||||
@Before
|
@BeforeClass
|
||||||
public void before() throws Exception {
|
public static void before() throws Exception {
|
||||||
System.out.println("Setting up!");
|
System.out.println("Setting up!");
|
||||||
wireMockServer.start();
|
wireMockServer.start();
|
||||||
configureFor("localhost", 8080);
|
configureFor("localhost", 8080);
|
||||||
stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn(
|
stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn(
|
||||||
aResponse().withStatus(200)
|
aResponse().withStatus(200)
|
||||||
.withHeader("Content-Type", APPLICATION_JSON)
|
.withHeader("Content-Type", APPLICATION_JSON)
|
||||||
.withBody(GAME_ODDS)));
|
.withBody(GAME_ODDS)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUrl_whenCheckingFloatValuePasses_thenCorrect() {
|
public void givenUrl_whenCheckingFloatValuePasses_thenCorrect() {
|
||||||
get("/events?id=390").then().assertThat()
|
get("/events?id=390").then().assertThat()
|
||||||
.body("odd.ck", equalTo(12.2f));
|
.body("odd.ck", equalTo(12.2f));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUrl_whenSuccessOnGetsResponseAndJsonHasRequiredKV_thenCorrect() {
|
public void givenUrl_whenSuccessOnGetsResponseAndJsonHasRequiredKV_thenCorrect() {
|
||||||
|
|
||||||
get("/events?id=390").then().statusCode(200).assertThat()
|
get("/events?id=390").then().statusCode(200).assertThat()
|
||||||
.body("id", equalTo("390"));
|
.body("id", equalTo("390"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
@Test
|
||||||
|
public void givenUrl_whenJsonResponseHasArrayWithGivenValuesUnderKey_thenCorrect() {
|
||||||
|
get("/events?id=390").then().assertThat()
|
||||||
|
.body("odds.price", hasItems("1.30", "5.25", "2.70", "1.20"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUrl_whenJsonResponseHasArrayWithGivenValuesUnderKey_thenCorrect() {
|
public void givenUrl_whenJsonResponseConformsToSchema_thenCorrect() {
|
||||||
get("/events?id=390").then().assertThat()
|
|
||||||
.body("odds.price", hasItems("1.30", "5.25", "2.70", "1.20"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
get("/events?id=390").then().assertThat()
|
||||||
public void givenUrl_whenJsonResponseConformsToSchema_thenCorrect() {
|
.body(matchesJsonSchemaInClasspath("event_0.json"));
|
||||||
|
}
|
||||||
|
|
||||||
get("/events?id=390").then().assertThat()
|
@Test
|
||||||
.body(matchesJsonSchemaInClasspath("event_0.json"));
|
public void givenUrl_whenValidatesResponseWithInstanceSettings_thenCorrect() {
|
||||||
}
|
JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory
|
||||||
|
.newBuilder()
|
||||||
|
.setValidationConfiguration(
|
||||||
|
ValidationConfiguration.newBuilder()
|
||||||
|
.setDefaultVersion(SchemaVersion.DRAFTV4)
|
||||||
|
.freeze()).freeze();
|
||||||
|
|
||||||
@Test
|
get("/events?id=390")
|
||||||
public void givenUrl_whenValidatesResponseWithInstanceSettings_thenCorrect() {
|
.then()
|
||||||
JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory
|
.assertThat()
|
||||||
.newBuilder()
|
.body(matchesJsonSchemaInClasspath("event_0.json").using(
|
||||||
.setValidationConfiguration(
|
jsonSchemaFactory));
|
||||||
ValidationConfiguration.newBuilder()
|
}
|
||||||
.setDefaultVersion(SchemaVersion.DRAFTV4)
|
|
||||||
.freeze()).freeze();
|
|
||||||
|
|
||||||
get("/events?id=390")
|
@Test
|
||||||
.then()
|
public void givenUrl_whenValidatesResponseWithStaticSettings_thenCorrect() {
|
||||||
.assertThat()
|
|
||||||
.body(matchesJsonSchemaInClasspath("event_0.json").using(
|
|
||||||
jsonSchemaFactory));
|
|
||||||
|
|
||||||
}
|
get("/events?id=390")
|
||||||
|
.then()
|
||||||
|
.assertThat()
|
||||||
|
.body(matchesJsonSchemaInClasspath("event_0.json").using(
|
||||||
|
settings().with().checkedValidation(false)));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@AfterClass
|
||||||
public void givenUrl_whenValidatesResponseWithStaticSettings_thenCorrect() {
|
public static void after() throws Exception {
|
||||||
|
System.out.println("Running: tearDown");
|
||||||
get("/events?id=390")
|
wireMockServer.stop();
|
||||||
.then()
|
}
|
||||||
.assertThat()
|
|
||||||
.body(matchesJsonSchemaInClasspath("event_0.json").using(
|
|
||||||
settings().with().checkedValidation(false)));
|
|
||||||
}
|
|
||||||
|
|
||||||
@After
|
|
||||||
public void after() throws Exception {
|
|
||||||
System.out.println("Running: tearDown");
|
|
||||||
wireMockServer.stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String getEventJson() {
|
|
||||||
return Util.inputStreamToString(RestAssuredIntegrationTest.class
|
|
||||||
.getResourceAsStream("/event_0.json"));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
private static String getEventJson() {
|
||||||
|
return Util.inputStreamToString(RestAssuredIntegrationTest.class
|
||||||
|
.getResourceAsStream("/event_0.json"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,54 +1,52 @@
|
||||||
package com.baeldung.restassured;
|
package com.baeldung.restassured;
|
||||||
|
|
||||||
|
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||||
|
import org.junit.AfterClass;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
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.configureFor;
|
||||||
import static com.github.tomakehurst.wiremock.client.WireMock.get;
|
import static com.github.tomakehurst.wiremock.client.WireMock.get;
|
||||||
import static com.github.tomakehurst.wiremock.client.WireMock.post;
|
|
||||||
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
|
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.urlEqualTo;
|
||||||
|
import static io.restassured.RestAssured.get;
|
||||||
import static org.hamcrest.Matchers.hasItems;
|
import static org.hamcrest.Matchers.hasItems;
|
||||||
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
import static io.restassured.RestAssured.get;
|
|
||||||
|
|
||||||
import com.github.tomakehurst.wiremock.WireMockServer;
|
|
||||||
|
|
||||||
public class RestAssuredXML2IntegrationTest {
|
public class RestAssuredXML2IntegrationTest {
|
||||||
private WireMockServer wireMockServer = new WireMockServer();
|
private static WireMockServer wireMockServer = new WireMockServer();
|
||||||
|
|
||||||
private static final String EVENTS_PATH = "/teachers";
|
private static final String EVENTS_PATH = "/teachers";
|
||||||
private static final String APPLICATION_XML = "application/xml";
|
private static final String APPLICATION_XML = "application/xml";
|
||||||
private static final String TEACHERS = getXml();
|
private static final String TEACHERS = getXml();
|
||||||
|
|
||||||
@Before
|
@BeforeClass
|
||||||
public void before() throws Exception {
|
public static void before() throws Exception {
|
||||||
System.out.println("Setting up!");
|
System.out.println("Setting up!");
|
||||||
wireMockServer.start();
|
wireMockServer.start();
|
||||||
configureFor("localhost", 8080);
|
configureFor("localhost", 8080);
|
||||||
stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn(
|
stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn(
|
||||||
aResponse().withStatus(200)
|
aResponse().withStatus(200)
|
||||||
.withHeader("Content-Type", APPLICATION_XML)
|
.withHeader("Content-Type", APPLICATION_XML)
|
||||||
.withBody(TEACHERS)));
|
.withBody(TEACHERS)));
|
||||||
}
|
}
|
||||||
@Test
|
|
||||||
public void givenUrl_whenVerifiesScienceTeacherFromXml_thenCorrect() {
|
|
||||||
get("/teachers")
|
|
||||||
.then()
|
|
||||||
.body("teachers.teacher.find { it.@department == 'science' }.subject",
|
|
||||||
hasItems("math", "physics"));
|
|
||||||
}
|
|
||||||
private static String getXml() {
|
|
||||||
|
|
||||||
return Util
|
|
||||||
.inputStreamToString(new RestAssuredXML2IntegrationTest().getClass().getResourceAsStream("/teachers.xml"));
|
|
||||||
|
|
||||||
}
|
|
||||||
@After
|
|
||||||
public void after() throws Exception {
|
|
||||||
System.out.println("Running: tearDown");
|
|
||||||
wireMockServer.stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUrl_whenVerifiesScienceTeacherFromXml_thenCorrect() {
|
||||||
|
get("/teachers")
|
||||||
|
.then()
|
||||||
|
.body("teachers.teacher.find { it.@department == 'science' }.subject",
|
||||||
|
hasItems("math", "physics"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getXml() {
|
||||||
|
return Util.inputStreamToString(RestAssuredXML2IntegrationTest.class
|
||||||
|
.getResourceAsStream("/teachers.xml"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterClass
|
||||||
|
public static void after() throws Exception {
|
||||||
|
System.out.println("Running: tearDown");
|
||||||
|
wireMockServer.stop();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,99 +1,86 @@
|
||||||
package com.baeldung.restassured;
|
package com.baeldung.restassured;
|
||||||
|
|
||||||
|
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||||
|
import org.junit.AfterClass;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
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.configureFor;
|
||||||
import static com.github.tomakehurst.wiremock.client.WireMock.get;
|
|
||||||
import static com.github.tomakehurst.wiremock.client.WireMock.post;
|
import static com.github.tomakehurst.wiremock.client.WireMock.post;
|
||||||
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
|
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.urlEqualTo;
|
||||||
import static io.restassured.RestAssured.post;
|
import static io.restassured.RestAssured.post;
|
||||||
import static io.restassured.RestAssured.get;
|
|
||||||
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 static org.hamcrest.Matchers.containsString;
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.xml.HasXPath.hasXPath;
|
import static org.hamcrest.xml.HasXPath.hasXPath;
|
||||||
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Before;
|
|
||||||
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;
|
|
||||||
public class RestAssuredXMLIntegrationTest {
|
public class RestAssuredXMLIntegrationTest {
|
||||||
private WireMockServer wireMockServer = new WireMockServer();
|
private static WireMockServer wireMockServer = new WireMockServer();
|
||||||
private static final String EVENTS_PATH = "/employees";
|
private static final String EVENTS_PATH = "/employees";
|
||||||
private static final String APPLICATION_XML = "application/xml";
|
private static final String APPLICATION_XML = "application/xml";
|
||||||
private static final String EMPLOYEES = getXml();
|
private static final String EMPLOYEES = getXml();
|
||||||
|
|
||||||
@Before
|
@BeforeClass
|
||||||
public void before() throws Exception {
|
public static void before() throws Exception {
|
||||||
System.out.println("Setting up!");
|
System.out.println("Setting up!");
|
||||||
wireMockServer.start();
|
wireMockServer.start();
|
||||||
configureFor("localhost", 8080);
|
configureFor("localhost", 8080);
|
||||||
stubFor(post(urlEqualTo(EVENTS_PATH)).willReturn(
|
stubFor(post(urlEqualTo(EVENTS_PATH)).willReturn(
|
||||||
aResponse().withStatus(200)
|
aResponse().withStatus(200)
|
||||||
.withHeader("Content-Type", APPLICATION_XML)
|
.withHeader("Content-Type", APPLICATION_XML)
|
||||||
.withBody(EMPLOYEES)));
|
.withBody(EMPLOYEES)));
|
||||||
}
|
}
|
||||||
@Test
|
|
||||||
public void givenUrl_whenXmlResponseValueTestsEqual_thenCorrect() {
|
|
||||||
post("/employees").then().assertThat()
|
|
||||||
.body("employees.employee.first-name", equalTo("Jane"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUrl_whenMultipleXmlValuesTestEqual_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"));
|
||||||
.body("employees.employee.last-name", equalTo("Daisy"))
|
}
|
||||||
.body("employees.employee.sex", equalTo("f"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUrl_whenMultipleXmlValuesTestEqualInShortHand_thenCorrect() {
|
public void givenUrl_whenMultipleXmlValuesTestEqual_thenCorrect() {
|
||||||
post("/employees")
|
post("/employees").then().assertThat()
|
||||||
.then()
|
.body("employees.employee.first-name", equalTo("Jane"))
|
||||||
.assertThat()
|
.body("employees.employee.last-name", equalTo("Daisy"))
|
||||||
.body("employees.employee.first-name", equalTo("Jane"),
|
.body("employees.employee.sex", equalTo("f"));
|
||||||
"employees.employee.last-name", equalTo("Daisy"),
|
}
|
||||||
"employees.employee.sex", equalTo("f"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUrl_whenValidatesXmlUsingXpath_thenCorrect() {
|
public void givenUrl_whenMultipleXmlValuesTestEqualInShortHand_thenCorrect() {
|
||||||
post("/employees")
|
post("/employees")
|
||||||
.then()
|
.then()
|
||||||
.assertThat()
|
.assertThat()
|
||||||
.body(hasXPath("/employees/employee/first-name",
|
.body("employees.employee.first-name", equalTo("Jane"),
|
||||||
containsString("Ja")));
|
"employees.employee.last-name", equalTo("Daisy"),
|
||||||
|
"employees.employee.sex", equalTo("f"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
@Test
|
||||||
|
public void givenUrl_whenValidatesXmlUsingXpath_thenCorrect() {
|
||||||
|
post("/employees")
|
||||||
|
.then()
|
||||||
|
.assertThat()
|
||||||
|
.body(hasXPath("/employees/employee/first-name",
|
||||||
|
containsString("Ja")));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUrl_whenValidatesXmlUsingXpath2_thenCorrect() {
|
public void givenUrl_whenValidatesXmlUsingXpath2_thenCorrect() {
|
||||||
post("/employees")
|
post("/employees")
|
||||||
.then()
|
.then()
|
||||||
.assertThat()
|
.assertThat()
|
||||||
.body(hasXPath("/employees/employee/first-name[text()='Jane']"));
|
.body(hasXPath("/employees/employee/first-name[text()='Jane']"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
private static String getXml() {
|
||||||
|
return Util
|
||||||
|
.inputStreamToString(RestAssuredXMLIntegrationTest.class.getResourceAsStream("/employees.xml"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterClass
|
||||||
private static String getXml() {
|
public static void after() throws Exception {
|
||||||
|
System.out.println("Running: tearDown");
|
||||||
return Util
|
wireMockServer.stop();
|
||||||
.inputStreamToString(new RestAssuredXMLIntegrationTest().getClass().getResourceAsStream("/employees.xml"));
|
}
|
||||||
|
|
||||||
}
|
|
||||||
@After
|
|
||||||
public void after() throws Exception {
|
|
||||||
System.out.println("Running: tearDown");
|
|
||||||
wireMockServer.stop();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,36 +1,15 @@
|
||||||
package com.baeldung.restassured;
|
package com.baeldung.restassured;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class Util {
|
final class Util {
|
||||||
public static String inputStreamToString(InputStream is) {
|
|
||||||
BufferedReader br = null;
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
|
|
||||||
String line;
|
private Util() {
|
||||||
try {
|
}
|
||||||
|
|
||||||
br = new BufferedReader(new InputStreamReader(is));
|
static String inputStreamToString(InputStream is) {
|
||||||
while ((line = br.readLine()) != null) {
|
Scanner s = new Scanner(is).useDelimiter("\\A");
|
||||||
sb.append(line);
|
return s.hasNext() ? s.next() : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} finally {
|
|
||||||
if (br != null) {
|
|
||||||
try {
|
|
||||||
br.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return sb.toString();
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue