Skip to content
CodeKruPro

Validate Array Size

9/62

Validate Array Size

When an endpoint returns a JSON array, you can validate its size:

.body("size()", equalTo(5))  // GPath expression
.body("$", hasSize(5))       // Hamcrest hasSize()

The API has exactly 5 users.

Task:

  1. GET /users and validate the array size is 5
  2. Print the confirmation

Expected output:

Total users: 5

About this lesson

REST Assured JSON array size: checking element count with hasSize and size()

Learn REST AssuredBeginnerResponse Body Validation

How it works

When the top of a response is a JSON array, the path for the whole body is the root. REST Assured accepts $ or an empty string for it, and Hamcrest's hasSize() then checks how many elements the parsed list holds. On failure the message prints the entire list it received, which shows at a glance which element is missing or extra.

The other route is GPath, the Groovy path language behind REST Assured's JSON paths. A path ending in size() calls Groovy's size method on whatever the path reaches, so size() on its own counts the root array and name.size() counts the collected names. Paired with equalTo(), this route gives a shorter failure message than hasSize(): just the two numbers.

Watch the shape of the target. On a single object, size() counts its keys, so user 1 reports five, the same as the user list.

About the assignment

The practice API holds exactly five users. You fetch the full list, assert that the array has that many elements, and print a total line once the count is confirmed.

What you will do

  1. Send a GET to /users through a given() chain.
  2. In then(), check the count with either size() as the path plus equalTo(), or the root path plus hasSize().
  3. Let the assertion finish the statement.
  4. Print the total line exactly as the task shows it.

Common mistakes

  • Counting the wrong endpoint

    Pointing a size check at /users/1 still passes with five, because size() on an object counts its keys. Make sure the path reaches the array you mean.

  • Using hasSize on a single value

    hasSize() needs a collection. Aim it at a path such as name on one product and it fails with "a collection with size", showing the plain string it got instead. Point it at the array or a collected field.

  • Hard-coding counts on live data

    This dataset is fixed, but real APIs grow. For changing data, prefer bounds such as greaterThan() inside hasSize(), or check for specific items instead.

In a real test suite

Size checks are common on list and search endpoints, often paired with a page-size parameter so the test can confirm the API honours it. In JUnit 5 the expected count usually comes from a constant or the test data setup rather than a literal in the assertion.

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.

Main.java
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

public class Main {
    public static void main(String[] args) {
        RestAssured.baseURI = "https://testkru.com/api";

        // TODO: GET /users and validate the array has exactly 5 elements
        // Use .body("size()", equalTo(5)) or .body("$", hasSize(5))
        // TODO: Print "Total users: 5"
    }
}