Rest-assured fix (#2759)

This commit is contained in:
Grzegorz Piwowarek 2017-10-19 17:32:02 +02:00 committed by GitHub
parent e6d82e8ced
commit fc0ed8fe3d
5 changed files with 218 additions and 262 deletions

View File

@ -1,29 +1,27 @@
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.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.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static io.restassured.RestAssured.get;
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 {
private WireMockServer wireMockServer = new WireMockServer();
private static WireMockServer wireMockServer = new WireMockServer();
private static final String EVENTS_PATH = "/odds";
private static final String APPLICATION_JSON = "application/json";
private static final String ODDS = getJson();
@Before
public void before() throws Exception {
@BeforeClass
public static void before() throws Exception {
System.out.println("Setting up!");
wireMockServer.start();
configureFor("localhost", 8080);
@ -41,15 +39,13 @@ public class RestAssured2IntegrationTest {
private static String getJson() {
return Util.inputStreamToString(new RestAssured2IntegrationTest().getClass()
return Util.inputStreamToString(RestAssured2IntegrationTest.class
.getResourceAsStream("/odds.json"));
}
@After
public void after() throws Exception {
@AfterClass
public static void after() throws Exception {
System.out.println("Running: tearDown");
wireMockServer.stop();
}
}

View File

@ -1,5 +1,13 @@
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.configureFor;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
@ -11,24 +19,15 @@ import static io.restassured.module.jsv.JsonSchemaValidatorSettings.settings;
import static org.hamcrest.Matchers.equalTo;
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 {
private WireMockServer wireMockServer = new WireMockServer();
private static 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 = getEventJson();
@Before
public void before() throws Exception {
@BeforeClass
public static void before() throws Exception {
System.out.println("Setting up!");
wireMockServer.start();
configureFor("localhost", 8080);
@ -49,7 +48,6 @@ public class RestAssuredIntegrationTest {
get("/events?id=390").then().statusCode(200).assertThat()
.body("id", equalTo("390"));
}
@Test
@ -79,7 +77,6 @@ public class RestAssuredIntegrationTest {
.assertThat()
.body(matchesJsonSchemaInClasspath("event_0.json").using(
jsonSchemaFactory));
}
@Test
@ -92,8 +89,8 @@ public class RestAssuredIntegrationTest {
settings().with().checkedValidation(false)));
}
@After
public void after() throws Exception {
@AfterClass
public static void after() throws Exception {
System.out.println("Running: tearDown");
wireMockServer.stop();
}
@ -102,5 +99,4 @@ public class RestAssuredIntegrationTest {
return Util.inputStreamToString(RestAssuredIntegrationTest.class
.getResourceAsStream("/event_0.json"));
}
}

View File

@ -1,29 +1,27 @@
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.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.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static io.restassured.RestAssured.get;
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 {
private WireMockServer wireMockServer = new WireMockServer();
private static WireMockServer wireMockServer = new WireMockServer();
private static final String EVENTS_PATH = "/teachers";
private static final String APPLICATION_XML = "application/xml";
private static final String TEACHERS = getXml();
@Before
public void before() throws Exception {
@BeforeClass
public static void before() throws Exception {
System.out.println("Setting up!");
wireMockServer.start();
configureFor("localhost", 8080);
@ -32,6 +30,7 @@ public class RestAssuredXML2IntegrationTest {
.withHeader("Content-Type", APPLICATION_XML)
.withBody(TEACHERS)));
}
@Test
public void givenUrl_whenVerifiesScienceTeacherFromXml_thenCorrect() {
get("/teachers")
@ -39,16 +38,15 @@ public class RestAssuredXML2IntegrationTest {
.body("teachers.teacher.find { it.@department == 'science' }.subject",
hasItems("math", "physics"));
}
private static String getXml() {
return Util
.inputStreamToString(new RestAssuredXML2IntegrationTest().getClass().getResourceAsStream("/teachers.xml"));
return Util.inputStreamToString(RestAssuredXML2IntegrationTest.class
.getResourceAsStream("/teachers.xml"));
}
@After
public void after() throws Exception {
@AfterClass
public static void after() throws Exception {
System.out.println("Running: tearDown");
wireMockServer.stop();
}
}

View File

@ -1,38 +1,28 @@
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.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.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
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.equalTo;
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 {
private WireMockServer wireMockServer = new WireMockServer();
private static WireMockServer wireMockServer = new WireMockServer();
private static final String EVENTS_PATH = "/employees";
private static final String APPLICATION_XML = "application/xml";
private static final String EMPLOYEES = getXml();
@Before
public void before() throws Exception {
@BeforeClass
public static void before() throws Exception {
System.out.println("Setting up!");
wireMockServer.start();
configureFor("localhost", 8080);
@ -41,6 +31,7 @@ public class RestAssuredXMLIntegrationTest {
.withHeader("Content-Type", APPLICATION_XML)
.withBody(EMPLOYEES)));
}
@Test
public void givenUrl_whenXmlResponseValueTestsEqual_thenCorrect() {
post("/employees").then().assertThat()
@ -72,7 +63,6 @@ public class RestAssuredXMLIntegrationTest {
.assertThat()
.body(hasXPath("/employees/employee/first-name",
containsString("Ja")));
}
@Test
@ -81,18 +71,15 @@ public class RestAssuredXMLIntegrationTest {
.then()
.assertThat()
.body(hasXPath("/employees/employee/first-name[text()='Jane']"));
}
private static String getXml() {
return Util
.inputStreamToString(new RestAssuredXMLIntegrationTest().getClass().getResourceAsStream("/employees.xml"));
.inputStreamToString(RestAssuredXMLIntegrationTest.class.getResourceAsStream("/employees.xml"));
}
@After
public void after() throws Exception {
@AfterClass
public static void after() throws Exception {
System.out.println("Running: tearDown");
wireMockServer.stop();
}

View File

@ -1,36 +1,15 @@
package com.baeldung.restassured;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Util {
public static String inputStreamToString(InputStream is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
final class Util {
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
private Util() {
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
static String inputStreamToString(InputStream is) {
Scanner s = new Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
}