I have REST for finding users by name, which for some search term returns users that has that term in first name or last name.
GET /search/joe
returns json array:
[
{"id": 1, "firstName": "Joe", "lastName": "Doe"},
{"id": 2, "firstName": "Alex", "lastName": "Joelson"}
]
How to test this REST with restassured and verify that given search term is contained in either first or last name of every row, case insensitive?
given()
.when()
.get("/user/search")
.then()
.statusCode(200)
.body(?)
Without a User
object:
Response response = given().when().get("/user/search");
response.then().statusCode(200);
JSONArray users = new JSONArray(response.asString());
for (int i = 0; i < users.length(); i++) {
JSONObject user = users.getJSONObject(i);
String firstName = user.getString("firstName").toLowercase();
String lastName = user.getString("lastName").toLowercase();
Assert.assertTrue(firstName.contains("joe") || lastName.contains("joe"));
}
If you have a User
object, look into using Jackson or JsonPath to make the validation simpler. You can also look into using Hamcrest to do the validations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With