About this lesson
REST Assured pagination test: page, limit and the meta block
Learn REST AssuredIntermediateQuery Parameters and Search
How it works
Large collections are served in slices. A client asks for a page number and a page size, and the server answers with that slice plus metadata so the caller knows how many pages exist. On testkru /search the two inputs are page and limit, and the meta object reports page, per_page, total and last_page. The last page is the total divided by the limit, rounded up, and never less than one.
The server clamps bad input instead of rejecting it. A limit below one becomes one, a limit above fifty becomes fifty, and a page below one becomes the first page. Asking for a page beyond the end is not an error either: the status is still 200, data is an empty array and meta still shows the real total and last page. That makes paging easy to get wrong silently, which is exactly why it deserves tests.
Each queryParam() call adds one more pair, and you can chain as many as you need. Passing an int is fine because the value is an Object. To count the records actually returned, read data as a list with getList() and ask for its size().
About the assignment
Request the first page of unfiltered results with three items per page and report every pagination field together with the number of records on that page.
What you will do
- Chain two
queryParam()calls ongiven():pageset to 1 andlimitset to 3. - Send a GET to
/search. - Read
meta.page,meta.per_page,meta.totalandmeta.last_pagewithgetInt()and print them afterPage:,Per page:,Total:andLast page:. - Read
datawithgetList(), take itssize(), and print it afterResults on page:.
Common mistakes
Reading per_page as perPage
The key uses an underscore. A camel-case path does not exist, so
getInt()finds nothing to unbox and throws aNullPointerException. Copy keys from a real response rather than from the naming style of your Java code.Treating an empty page as a failure of the request
Page 99 still answers 200 with empty data. If your loop over pages stops only on an error status it will never stop. Stop when the page number passes
last_pageor whendatacomes back empty.Expecting an error for a huge limit
A limit of 500 is quietly reduced to 50 and
per_pagereports the reduced value. Assert onper_pagefrom the response, not on the number you sent, when the API documents a cap.
In a real test suite
A thorough pagination check walks every page, collects the ids into a set and asserts that the set size equals meta.total, which catches duplicates and gaps between pages. Edge cases worth one test each are the last partial page, a page past the end, and the smallest and largest allowed limits.
Related lessons
- Sorting ResultsCombines limit with a sort order on the same endpoint.
- Validate Array SizeCounts elements of a JSON array, as this lesson does for one page.
- Simple Text SearchFirst lesson to read meta.total from the search response.
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.*;
public class Main {
public static void main(String[] args) {
RestAssured.baseURI = "https://testkru.com/api";
// TODO: GET /search with queryParam "page" = 1 and "limit" = 3
// TODO: Print all meta fields and count results on this page
}
}