BAEL-9041 Let's add some examples to the rest-assured tutorial
-Add new POST with body example
This commit is contained in:
parent
1bca2a8c54
commit
9b743a9034
|
@ -0,0 +1,49 @@
|
|||
package com.baeldung.restassured;
|
||||
|
||||
public class Odd {
|
||||
|
||||
float price;
|
||||
int status;
|
||||
float ck;
|
||||
String name;
|
||||
|
||||
Odd(float price, int status, float ck, String name) {
|
||||
this.price = price;
|
||||
this.status = status;
|
||||
this.ck = ck;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public float getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(float price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public int getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(int status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public float getCk() {
|
||||
return ck;
|
||||
}
|
||||
|
||||
public void setCk(float ck) {
|
||||
this.ck = ck;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
|
@ -5,13 +5,15 @@ import io.restassured.RestAssured;
|
|||
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.containing;
|
||||
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 io.restassured.RestAssured.with;
|
||||
import static org.hamcrest.Matchers.hasItems;
|
||||
|
||||
public class RestAssured2IntegrationTest {
|
||||
|
@ -29,20 +31,32 @@ public class RestAssured2IntegrationTest {
|
|||
configureFor("localhost", PORT);
|
||||
RestAssured.port = PORT;
|
||||
stubFor(get(urlEqualTo(EVENTS_PATH)).willReturn(
|
||||
aResponse().withStatus(200)
|
||||
aResponse().withStatus(200)
|
||||
.withHeader("Content-Type", APPLICATION_JSON)
|
||||
.withBody(ODDS)));
|
||||
stubFor(post(urlEqualTo("/odds/new"))
|
||||
.withRequestBody(containing("{\"price\":5.25,\"status\":1,\"ck\":13.1,\"name\":\"X\"}"))
|
||||
.willReturn(aResponse().withStatus(201)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUrl_whenVerifiesOddPricesAccuratelyByStatus_thenCorrect() {
|
||||
get("/odds").then().body("odds.findAll { it.status > 0 }.price",
|
||||
hasItems(5.25f, 1.2f));
|
||||
get("/odds").then().body("odds.findAll { it.status > 0 }.price",
|
||||
hasItems(5.25f, 1.2f));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestedPost_thenCreated() {
|
||||
with().body(new Odd(5.25f, 1, 13.1f, "X"))
|
||||
.when()
|
||||
.request("POST", "/odds/new")
|
||||
.then()
|
||||
.statusCode(201);
|
||||
}
|
||||
|
||||
private static String getJson() {
|
||||
return Util.inputStreamToString(RestAssured2IntegrationTest.class
|
||||
.getResourceAsStream("/odds.json"));
|
||||
.getResourceAsStream("/odds.json"));
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
|
|
Loading…
Reference in New Issue