Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restassured "OR" condition

Tags:

rest-assured

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(?)
like image 985
petronic Avatar asked Oct 16 '25 14:10

petronic


1 Answers

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.

like image 89
Adam Avatar answered Oct 19 '25 15:10

Adam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!