In this post we will show the easiest way to test the request with returns a bunch of users. The main goal of the request is to sort the users by a specific field, so let’s dive in this problem.
What do we have? We have a request with will return a JSON with a list of user, how to we know if list is sorted or not? Here comes rest-assured Response functional to help us to parse the JSON.
We have our request and we will extract the response:
1 2 3 4 5 6 7 | RestAssured.basePath = "/admin/users";Response response = given() .queryParam("sort", "firstName") .contentType(ContentType.JSON) .accept(ContentType.JSON) .when().get() .then().statusCode(200).extract().response(); |
Next step is to extract the sorted values from JSON response in a List of Strings:
1 | List<String> jsonResponse = response.jsonPath().getList("firstName"); |
And the lest step check if the list of extracting elements is sorted using Ordering class. An Ordering is a Comparator++. In this case, if you have a list of some type that implements Comparable, you could write:
1 | assertTrue(Ordering.natural().isOrdered(jsonResponse)); |
Hope this will help you, don’t hesitate to ask questions, will answer with pleasure!