Skip to content

Commit 9b743a9

Browse files
committed
BAEL-9041 Let's add some examples to the rest-assured tutorial
-Add new POST with body example
1 parent 1bca2a8 commit 9b743a9

2 files changed

Lines changed: 68 additions & 5 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.restassured;
2+
3+
public class Odd {
4+
5+
float price;
6+
int status;
7+
float ck;
8+
String name;
9+
10+
Odd(float price, int status, float ck, String name) {
11+
this.price = price;
12+
this.status = status;
13+
this.ck = ck;
14+
this.name = name;
15+
}
16+
17+
public float getPrice() {
18+
return price;
19+
}
20+
21+
public void setPrice(float price) {
22+
this.price = price;
23+
}
24+
25+
public int getStatus() {
26+
return status;
27+
}
28+
29+
public void setStatus(int status) {
30+
this.status = status;
31+
}
32+
33+
public float getCk() {
34+
return ck;
35+
}
36+
37+
public void setCk(float ck) {
38+
this.ck = ck;
39+
}
40+
41+
public String getName() {
42+
return name;
43+
}
44+
45+
public void setName(String name) {
46+
this.name = name;
47+
}
48+
49+
}

‎testing-modules/rest-assured/src/test/java/com/baeldung/restassured/RestAssured2IntegrationTest.java‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
import org.junit.AfterClass;
66
import org.junit.BeforeClass;
77
import org.junit.Test;
8-
98
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
109
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
10+
import static com.github.tomakehurst.wiremock.client.WireMock.containing;
1111
import static com.github.tomakehurst.wiremock.client.WireMock.get;
12+
import static com.github.tomakehurst.wiremock.client.WireMock.post;
1213
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
1314
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
1415
import static io.restassured.RestAssured.get;
16+
import static io.restassured.RestAssured.with;
1517
import static org.hamcrest.Matchers.hasItems;
1618

1719
public class RestAssured2IntegrationTest {
@@ -29,20 +31,32 @@ public static void before() throws Exception {
2931
configureFor("localhost", PORT);
3032
RestAssured.port = PORT;
3133
stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn(
32-
aResponse().withStatus(200)
34+
aResponse().withStatus(200)
3335
.withHeader("Content-Type", APPLICATION_JSON)
3436
.withBody(ODDS)));
37+
stubFor(post(urlEqualTo("/odds/new"))
38+
.withRequestBody(containing("{\"price\":5.25,\"status\":1,\"ck\":13.1,\"name\":\"X\"}"))
39+
.willReturn(aResponse().withStatus(201)));
3540
}
3641

3742
@Test
3843
public void givenUrl_whenVerifiesOddPricesAccuratelyByStatus_thenCorrect() {
39-
get("/odds").then().body("odds.findAll { it.status > 0 }.price",
40-
hasItems(5.25f, 1.2f));
44+
get("/odds").then().body("odds.findAll { it.status > 0 }.price",
45+
hasItems(5.25f, 1.2f));
46+
}
47+
48+
@Test
49+
public void whenRequestedPost_thenCreated() {
50+
with().body(new Odd(5.25f, 1, 13.1f, "X"))
51+
.when()
52+
.request("POST", "/odds/new")
53+
.then()
54+
.statusCode(201);
4155
}
4256

4357
private static String getJson() {
4458
return Util.inputStreamToString(RestAssured2IntegrationTest.class
45-
.getResourceAsStream("/odds.json"));
59+
.getResourceAsStream("/odds.json"));
4660
}
4761

4862
@AfterClass

0 commit comments

Comments
 (0)