artofcode.info

The beauty of coding!

REST-assured: how to check the user sorting

Author Avatar steti 03 Jan 2019
REST-assured: how to check the user sorting

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!

Frequently Asked Questions

No, you typically need to extract the list of values and use a Java library like Guava or standard Collections to verify the order.

How to Cite This Article

APA Format: steti. (2019). REST-assured: how to check the user sorting. Retrieved from https://artofcode.info/posts/rest-assured-how-to-check-the-user-sorting
MLA Format: steti. "REST-assured: how to check the user sorting." Steti.info, 03 Jan 2019. https://artofcode.info/posts/rest-assured-how-to-check-the-user-sorting
Chicago Format: steti. "REST-assured: how to check the user sorting." Steti.info. January 03, 2019. https://artofcode.info/posts/rest-assured-how-to-check-the-user-sorting
Back to Posts