Skip to content

Commit fc0ed8f

Browse files
authored
Rest-assured fix (eugenp#2759)
1 parent e6d82e8 commit fc0ed8f

5 files changed

Lines changed: 238 additions & 282 deletions

File tree

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,51 @@
11
package com.baeldung.restassured;
22

3+
import com.github.tomakehurst.wiremock.WireMockServer;
4+
import org.junit.AfterClass;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
38
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
49
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
510
import static com.github.tomakehurst.wiremock.client.WireMock.get;
6-
import static com.github.tomakehurst.wiremock.client.WireMock.post;
711
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
812
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
9-
import static org.hamcrest.Matchers.hasItems;
10-
11-
import org.junit.After;
12-
import org.junit.Before;
13-
import org.junit.Test;
1413
import static io.restassured.RestAssured.get;
15-
16-
import com.github.tomakehurst.wiremock.WireMockServer;
14+
import static org.hamcrest.Matchers.hasItems;
1715

1816
public class RestAssured2IntegrationTest {
19-
private WireMockServer wireMockServer = new WireMockServer();
20-
21-
private static final String EVENTS_PATH = "/odds";
22-
private static final String APPLICATION_JSON = "application/json";
23-
private static final String ODDS = getJson();
24-
25-
@Before
26-
public void before() throws Exception {
27-
System.out.println("Setting up!");
28-
wireMockServer.start();
29-
configureFor("localhost", 8080);
30-
stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn(
31-
aResponse().withStatus(200)
32-
.withHeader("Content-Type", APPLICATION_JSON)
33-
.withBody(ODDS)));
34-
}
35-
36-
@Test
37-
public void givenUrl_whenVerifiesOddPricesAccuratelyByStatus_thenCorrect() {
38-
get("/odds").then().body("odds.findAll { it.status > 0 }.price",
39-
hasItems(5.25f, 1.2f));
40-
}
41-
42-
private static String getJson() {
43-
44-
return Util.inputStreamToString(new RestAssured2IntegrationTest().getClass()
45-
.getResourceAsStream("/odds.json"));
46-
47-
}
48-
49-
@After
50-
public void after() throws Exception {
51-
System.out.println("Running: tearDown");
52-
wireMockServer.stop();
53-
}
54-
17+
private static WireMockServer wireMockServer = new WireMockServer();
18+
19+
private static final String EVENTS_PATH = "/odds";
20+
private static final String APPLICATION_JSON = "application/json";
21+
private static final String ODDS = getJson();
22+
23+
@BeforeClass
24+
public static void before() throws Exception {
25+
System.out.println("Setting up!");
26+
wireMockServer.start();
27+
configureFor("localhost", 8080);
28+
stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn(
29+
aResponse().withStatus(200)
30+
.withHeader("Content-Type", APPLICATION_JSON)
31+
.withBody(ODDS)));
32+
}
33+
34+
@Test
35+
public void givenUrl_whenVerifiesOddPricesAccuratelyByStatus_thenCorrect() {
36+
get("/odds").then().body("odds.findAll { it.status > 0 }.price",
37+
hasItems(5.25f, 1.2f));
38+
}
39+
40+
private static String getJson() {
41+
42+
return Util.inputStreamToString(RestAssured2IntegrationTest.class
43+
.getResourceAsStream("/odds.json"));
44+
}
45+
46+
@AfterClass
47+
public static void after() throws Exception {
48+
System.out.println("Running: tearDown");
49+
wireMockServer.stop();
50+
}
5551
}
Lines changed: 86 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
package com.baeldung.restassured;
22

3+
import com.github.fge.jsonschema.SchemaVersion;
4+
import com.github.fge.jsonschema.cfg.ValidationConfiguration;
5+
import com.github.fge.jsonschema.main.JsonSchemaFactory;
6+
import com.github.tomakehurst.wiremock.WireMockServer;
7+
import org.junit.AfterClass;
8+
import org.junit.BeforeClass;
9+
import org.junit.Test;
10+
311
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
412
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
513
import static com.github.tomakehurst.wiremock.client.WireMock.get;
@@ -11,96 +19,84 @@
1119
import static org.hamcrest.Matchers.equalTo;
1220
import static org.hamcrest.Matchers.hasItems;
1321

14-
import org.junit.After;
15-
import org.junit.Before;
16-
import org.junit.Test;
17-
18-
import com.github.fge.jsonschema.SchemaVersion;
19-
import com.github.fge.jsonschema.cfg.ValidationConfiguration;
20-
import com.github.fge.jsonschema.main.JsonSchemaFactory;
21-
import com.github.tomakehurst.wiremock.WireMockServer;
22-
2322
public class RestAssuredIntegrationTest {
2423

25-
private WireMockServer wireMockServer = new WireMockServer();
26-
private static final String EVENTS_PATH = "/events?id=390";
27-
private static final String APPLICATION_JSON = "application/json";
28-
private static final String GAME_ODDS = getEventJson();
29-
30-
@Before
31-
public void before() throws Exception {
32-
System.out.println("Setting up!");
33-
wireMockServer.start();
34-
configureFor("localhost", 8080);
35-
stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn(
36-
aResponse().withStatus(200)
37-
.withHeader("Content-Type", APPLICATION_JSON)
38-
.withBody(GAME_ODDS)));
39-
}
40-
41-
@Test
42-
public void givenUrl_whenCheckingFloatValuePasses_thenCorrect() {
43-
get("/events?id=390").then().assertThat()
44-
.body("odd.ck", equalTo(12.2f));
45-
}
46-
47-
@Test
48-
public void givenUrl_whenSuccessOnGetsResponseAndJsonHasRequiredKV_thenCorrect() {
49-
50-
get("/events?id=390").then().statusCode(200).assertThat()
51-
.body("id", equalTo("390"));
52-
53-
}
54-
55-
@Test
56-
public void givenUrl_whenJsonResponseHasArrayWithGivenValuesUnderKey_thenCorrect() {
57-
get("/events?id=390").then().assertThat()
58-
.body("odds.price", hasItems("1.30", "5.25", "2.70", "1.20"));
59-
}
60-
61-
@Test
62-
public void givenUrl_whenJsonResponseConformsToSchema_thenCorrect() {
63-
64-
get("/events?id=390").then().assertThat()
65-
.body(matchesJsonSchemaInClasspath("event_0.json"));
66-
}
67-
68-
@Test
69-
public void givenUrl_whenValidatesResponseWithInstanceSettings_thenCorrect() {
70-
JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory
71-
.newBuilder()
72-
.setValidationConfiguration(
73-
ValidationConfiguration.newBuilder()
74-
.setDefaultVersion(SchemaVersion.DRAFTV4)
75-
.freeze()).freeze();
76-
77-
get("/events?id=390")
78-
.then()
79-
.assertThat()
80-
.body(matchesJsonSchemaInClasspath("event_0.json").using(
81-
jsonSchemaFactory));
82-
83-
}
84-
85-
@Test
86-
public void givenUrl_whenValidatesResponseWithStaticSettings_thenCorrect() {
87-
88-
get("/events?id=390")
89-
.then()
90-
.assertThat()
91-
.body(matchesJsonSchemaInClasspath("event_0.json").using(
92-
settings().with().checkedValidation(false)));
93-
}
94-
95-
@After
96-
public void after() throws Exception {
97-
System.out.println("Running: tearDown");
98-
wireMockServer.stop();
99-
}
100-
101-
private static String getEventJson() {
102-
return Util.inputStreamToString(RestAssuredIntegrationTest.class
103-
.getResourceAsStream("/event_0.json"));
104-
}
105-
24+
private static WireMockServer wireMockServer = new WireMockServer();
25+
private static final String EVENTS_PATH = "/events?id=390";
26+
private static final String APPLICATION_JSON = "application/json";
27+
private static final String GAME_ODDS = getEventJson();
28+
29+
@BeforeClass
30+
public static void before() throws Exception {
31+
System.out.println("Setting up!");
32+
wireMockServer.start();
33+
configureFor("localhost", 8080);
34+
stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn(
35+
aResponse().withStatus(200)
36+
.withHeader("Content-Type", APPLICATION_JSON)
37+
.withBody(GAME_ODDS)));
38+
}
39+
40+
@Test
41+
public void givenUrl_whenCheckingFloatValuePasses_thenCorrect() {
42+
get("/events?id=390").then().assertThat()
43+
.body("odd.ck", equalTo(12.2f));
44+
}
45+
46+
@Test
47+
public void givenUrl_whenSuccessOnGetsResponseAndJsonHasRequiredKV_thenCorrect() {
48+
49+
get("/events?id=390").then().statusCode(200).assertThat()
50+
.body("id", equalTo("390"));
51+
}
52+
53+
@Test
54+
public void givenUrl_whenJsonResponseHasArrayWithGivenValuesUnderKey_thenCorrect() {
55+
get("/events?id=390").then().assertThat()
56+
.body("odds.price", hasItems("1.30", "5.25", "2.70", "1.20"));
57+
}
58+
59+
@Test
60+
public void givenUrl_whenJsonResponseConformsToSchema_thenCorrect() {
61+
62+
get("/events?id=390").then().assertThat()
63+
.body(matchesJsonSchemaInClasspath("event_0.json"));
64+
}
65+
66+
@Test
67+
public void givenUrl_whenValidatesResponseWithInstanceSettings_thenCorrect() {
68+
JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory
69+
.newBuilder()
70+
.setValidationConfiguration(
71+
ValidationConfiguration.newBuilder()
72+
.setDefaultVersion(SchemaVersion.DRAFTV4)
73+
.freeze()).freeze();
74+
75+
get("/events?id=390")
76+
.then()
77+
.assertThat()
78+
.body(matchesJsonSchemaInClasspath("event_0.json").using(
79+
jsonSchemaFactory));
80+
}
81+
82+
@Test
83+
public void givenUrl_whenValidatesResponseWithStaticSettings_thenCorrect() {
84+
85+
get("/events?id=390")
86+
.then()
87+
.assertThat()
88+
.body(matchesJsonSchemaInClasspath("event_0.json").using(
89+
settings().with().checkedValidation(false)));
90+
}
91+
92+
@AfterClass
93+
public static void after() throws Exception {
94+
System.out.println("Running: tearDown");
95+
wireMockServer.stop();
96+
}
97+
98+
private static String getEventJson() {
99+
return Util.inputStreamToString(RestAssuredIntegrationTest.class
100+
.getResourceAsStream("/event_0.json"));
101+
}
106102
}
Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,52 @@
11
package com.baeldung.restassured;
22

3+
import com.github.tomakehurst.wiremock.WireMockServer;
4+
import org.junit.AfterClass;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
38
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
49
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
510
import static com.github.tomakehurst.wiremock.client.WireMock.get;
6-
import static com.github.tomakehurst.wiremock.client.WireMock.post;
711
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
812
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
13+
import static io.restassured.RestAssured.get;
914
import static org.hamcrest.Matchers.hasItems;
1015

11-
import org.junit.After;
12-
import org.junit.Before;
13-
import org.junit.Test;
14-
import static io.restassured.RestAssured.get;
16+
public class RestAssuredXML2IntegrationTest {
17+
private static WireMockServer wireMockServer = new WireMockServer();
1518

16-
import com.github.tomakehurst.wiremock.WireMockServer;
19+
private static final String EVENTS_PATH = "/teachers";
20+
private static final String APPLICATION_XML = "application/xml";
21+
private static final String TEACHERS = getXml();
1722

18-
public class RestAssuredXML2IntegrationTest {
19-
private WireMockServer wireMockServer = new WireMockServer();
20-
21-
private static final String EVENTS_PATH = "/teachers";
22-
private static final String APPLICATION_XML = "application/xml";
23-
private static final String TEACHERS = getXml();
24-
25-
@Before
26-
public void before() throws Exception {
27-
System.out.println("Setting up!");
28-
wireMockServer.start();
29-
configureFor("localhost", 8080);
30-
stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn(
31-
aResponse().withStatus(200)
32-
.withHeader("Content-Type", APPLICATION_XML)
33-
.withBody(TEACHERS)));
34-
}
35-
@Test
36-
public void givenUrl_whenVerifiesScienceTeacherFromXml_thenCorrect() {
37-
get("/teachers")
38-
.then()
39-
.body("teachers.teacher.find { it.@department == 'science' }.subject",
40-
hasItems("math", "physics"));
41-
}
42-
private static String getXml() {
43-
44-
return Util
45-
.inputStreamToString(new RestAssuredXML2IntegrationTest().getClass().getResourceAsStream("/teachers.xml"));
46-
47-
}
48-
@After
49-
public void after() throws Exception {
50-
System.out.println("Running: tearDown");
51-
wireMockServer.stop();
52-
}
23+
@BeforeClass
24+
public static void before() throws Exception {
25+
System.out.println("Setting up!");
26+
wireMockServer.start();
27+
configureFor("localhost", 8080);
28+
stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn(
29+
aResponse().withStatus(200)
30+
.withHeader("Content-Type", APPLICATION_XML)
31+
.withBody(TEACHERS)));
32+
}
33+
34+
@Test
35+
public void givenUrl_whenVerifiesScienceTeacherFromXml_thenCorrect() {
36+
get("/teachers")
37+
.then()
38+
.body("teachers.teacher.find { it.@department == 'science' }.subject",
39+
hasItems("math", "physics"));
40+
}
41+
42+
private static String getXml() {
43+
return Util.inputStreamToString(RestAssuredXML2IntegrationTest.class
44+
.getResourceAsStream("/teachers.xml"));
45+
}
5346

47+
@AfterClass
48+
public static void after() throws Exception {
49+
System.out.println("Running: tearDown");
50+
wireMockServer.stop();
51+
}
5452
}

0 commit comments

Comments
 (0)