1

How can I check the order of a response body (array) in Rest-assured.

Response body is like:

[
    {"name": "a"},
    {"name":"b"}
]

2 Answers2

1
RestAssured.get("pictures?sort=name")
                .then()
                .body("[0].name", response -> Matchers.lessThanOrEqualTo(response.path("[1].name")))

Explanation: the body method gets the value (by path) of element [0].name then uses a ResponseAwareMatcher (lambda) to match the [0].name value with the value [1].name (by path) in the response.

0

We can use the jsonPath method in Rest Assured to check the order of the response body. You can retrieve the values of the "name" field for each element in the array and then compare the order of the retrieved values to the expected order. For example:

List<String> names = 
  given().
  when().
    get("<your API endpoint here>").
  then().
    extract().
    jsonPath().getList("$.name");

assertEquals(Arrays.asList("a", "b"), names);

This code retrieves the "name" field values for each element in the array and saves them as a list of strings. Then, it compares the retrieved values to the expected values using the assertEquals method. If the order of the values in the response body and the expected values match, the assertion will pass.