About this lesson
REST Assured sort query parameter: ordering search results on the server
Learn REST AssuredIntermediateQuery Parameters and Search
How it works
When an API offers sorting, let the server do it. The sort is applied to the whole result set before the page is cut, so the first page of a descending query really holds the highest values overall. Sorting in your test code after the fact only reorders the few records that happened to arrive, which is a different question.
testkru /search accepts sort with the value asc (the default) or desc, and it orders by name. The parameter is lowercased, so DESC works too, and any other word falls back to ascending without an error. The comparison is byte by byte, which means capital letters come before lower-case ones and digits before both. With fifteen names in the dataset, a descending sort puts the names that start latest in the alphabet at the front.
Combine sort with limit to fetch only the top few entries. Then pull the names with getList("data.name"), which returns them in exactly the order the server sent, ready to print or to compare with Hamcrest contains().
About the assignment
Fetch only the first three search results in descending name order and print their names as one list.
What you will do
- Add a
sortparameter set to desc withqueryParam(). - Add a
limitparameter set to 3 in the same chain. - Send a GET to
/searchand collectdata.nameinto aList<String>withgetList(). - Print the list after the label
First 3 (descending):.
Common mistakes
Reversing the default page yourself
Without the sort parameter you receive the first five names in ascending order. Reversing those five gives names from the start of the alphabet, not the end, so the result looks plausible and is wrong.
Misspelling the sort value
A value such as
descendingis not rejected. The server falls back to ascending and still returns 200, so assert on the actual order of the names, not just on the status code.Expecting dictionary order for mixed case
Byte ordering is not the same as a human dictionary. If a dataset mixes upper- and lower-case first letters, the order can surprise you. Check what the API documents before writing the expected list.
In a real test suite
Sorting bugs often appear only with real data, so suites typically assert a general property in addition to one fixed example: take the returned names, copy them, sort the copy with the same rule, and assert both lists are equal. Doing that for both directions and with paging switched on catches a server that sorts each page separately instead of the whole set.
Related lessons
- PaginationExplains limit and the paging metadata used alongside sort.
- Filter by CategoryPrints a list of names from the same endpoint in default ascending order.
- hasItem and hasItemsShows Hamcrest collection matchers for checking list contents.
To try one of these calls on any API endpoint without a task, open the Online Selenium Java Compiler (REST Assured is included).
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import static io.restassured.RestAssured.*;
import java.util.List;
public class Main {
public static void main(String[] args) {
RestAssured.baseURI = "https://testkru.com/api";
// TODO: GET /search with sort=desc and limit=3
// TODO: Extract the names and print them
}
}