Q23 of 40 · REST Assured

Explain how you'd test pagination with REST Assured.

REST AssuredMidrest-assuredpaginationapi-testingtest-coverage

Short answer

Short answer: Fetch the first page, assert page metadata (size, total, hasNext), extract items, then loop through remaining pages. Accumulate results and assert total count matches the declared total. Also test boundary cases: page 0 (or 1), last page, and an out-of-bounds page number returning an empty list or 404.

Detail

Pagination tests have two concerns: per-page correctness (each page has the right items and the right metadata) and completeness (all pages together cover the full dataset).

Per-page assertions:

given(reqSpec).queryParam("page", 0).queryParam("size", 10)
.when().get("/products")
.then()
    .statusCode(200)
    .body("content.size()", lessThanOrEqualTo(10))
    .body("page.number",    equalTo(0))
    .body("page.size",      equalTo(10))
    .body("page.totalElements", greaterThan(0));

Completeness loop — fetch all pages and accumulate:

List<String> allIds = new ArrayList<>();
boolean hasMore = true;
int page = 0;
while (hasMore) {
    Response r = given(reqSpec).queryParams("page", page, "size", 20)
        .when().get("/products").then().statusCode(200).extract().response();
    allIds.addAll(r.jsonPath().getList("content.id", String.class));
    hasMore = !r.jsonPath().getBoolean("last");
    page++;
}
assertThat(allIds).doesNotHaveDuplicates();
assertThat(allIds).hasSize(expectedTotal);

Boundary cases: page=-1 (400), page=99999 (empty content, not 404), size=0 (400 or empty), size=1000 (server cap).

// EXAMPLE

@Test
void pagination_firstPageHasCorrectMetadata() {
    given(reqSpec)
        .queryParam("page", 0)
        .queryParam("size", 5)
    .when()
        .get("/orders")
    .then()
        .statusCode(200)
        .body("content.size()",       equalTo(5))
        .body("page.number",          equalTo(0))
        .body("page.size",            equalTo(5))
        .body("page.totalElements",   greaterThanOrEqualTo(5))
        .body("page.totalPages",      greaterThanOrEqualTo(1));
}

@Test
void pagination_lastPageHasNoNextLink() {
    // Get total pages first
    int totalPages = given(reqSpec).queryParam("page", 0).queryParam("size", 100)
        .when().get("/orders").then().extract().path("page.totalPages");

    given(reqSpec)
        .queryParam("page", totalPages - 1)
        .queryParam("size", 100)
    .when()
        .get("/orders")
    .then()
        .statusCode(200)
        .body("last", equalTo(true));
}

// WHAT INTERVIEWERS LOOK FOR

Testing pagination metadata (not just items), accumulation loop for completeness, and boundary cases (out-of-bounds page, size limits). Checking for duplicate IDs across pages is a strong quality signal.

// COMMON PITFALL

Only testing page 0 and asserting it returns items. That verifies the endpoint exists, not that pagination actually works — you need to assert metadata fields and test multiple pages.