Im posting simple data like
{
"title" : "test Title"
}
to (let it be) /posts uri, eg smth like
RestAssured.baseURI = "http://localhost";
RestAssured.basePath = "/posts";
given()
.contentType("application/json")
.body("{\n" +
" \"title\": \"test Title\"\n" +
"}")
.when()
.post("")
.then().statusCode(201)
// .and(Verify that post created);
}
I can verify, that body is not null with
.body(notNullValue())
or check, that field in response has value we are setting, like
.body("title", equalTo("test Title"))
But im not sure, its optimal/correct way. So, question: how to verify, that entity was created, after posting, with restAssured?
You can validate the response content to make sure it's correct using jsonPath. Below response for a get request , but you can use it with some amendments
import io.restassured.http.ContentType;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
Response response=given().contentType(ContentType.JSON).get("http://localhost:3000/posts");
//we need to convert response as a String and give array index
JsonPath jsonPath = new JsonPath(response.asString());
String title = jsonPath.getString("title");
// use index if response returns an array
String author=jsonPath.getString("author[2]");
// if it's int
int user_id = jsonPath.getInt("user_id");
System.out.println("title is "+title+" customerName is "+author);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.