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.AfterClass;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
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.containing;
|
||||||
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 io.restassured.RestAssured.get;
|
||||||
|
import static io.restassured.RestAssured.with;
|
||||||
import static org.hamcrest.Matchers.hasItems;
|
import static org.hamcrest.Matchers.hasItems;
|
||||||
|
|
||||||
public class RestAssured2IntegrationTest {
|
public class RestAssured2IntegrationTest {
|
||||||
@ -32,6 +34,9 @@ public class RestAssured2IntegrationTest {
|
|||||||
aResponse().withStatus(200)
|
aResponse().withStatus(200)
|
||||||
.withHeader("Content-Type", APPLICATION_JSON)
|
.withHeader("Content-Type", APPLICATION_JSON)
|
||||||
.withBody(ODDS)));
|
.withBody(ODDS)));
|
||||||
|
stubFor(post(urlEqualTo("/odds/new"))
|
||||||
|
.withRequestBody(containing("{\"price\":5.25,\"status\":1,\"ck\":13.1,\"name\":\"X\"}"))
|
||||||
|
.willReturn(aResponse().withStatus(201)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -40,6 +45,15 @@ public class RestAssured2IntegrationTest {
|
|||||||
hasItems(5.25f, 1.2f));
|
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() {
|
private static String getJson() {
|
||||||
return Util.inputStreamToString(RestAssured2IntegrationTest.class
|
return Util.inputStreamToString(RestAssured2IntegrationTest.class
|
||||||
.getResourceAsStream("/odds.json"));
|
.getResourceAsStream("/odds.json"));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user