← Back to Blog

January 03, 2019 · 4 views

REST-assured: how to check the user sorting

Testing Automation
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

How do I verify sorting in an API response using Rest-assured?

You can extract the list of values from the JSON response using Rest-assured's JsonPath, and then use a library like AssertJ or standard Java collections to verify that the list is sorted in the expected order.

📚 How to Cite This Article

APA Format:

I enjoy building things that live on the internet. (2019). REST-assured: how to check the user sorting. Steti.info. https://steti.info/blog/rest-assured-how-to-check-the-user-sorting

MLA Format:

I enjoy building things that live on the internet. "REST-assured: how to check the user sorting." Steti.info, 03 Jan. 2019. https://steti.info/blog/rest-assured-how-to-check-the-user-sorting.

Chicago Style:

I enjoy building things that live on the internet. "REST-assured: how to check the user sorting." Steti.info. January 03, 2019. https://steti.info/blog/rest-assured-how-to-check-the-user-sorting.

Published: January 03, 2019
Last Updated: November 29, 2025

About the Author

Author
I like to build from websites to web apps, I create digital experiences that solve real problems and delight users and the most important is that all that I build, I build with PEOPLE!
Learn more about the author →

Related Posts