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
- Send a GET to
/usersthrough agiven()chain. - In
then(), check the count with eithersize()as the path plusequalTo(), or the root path plushasSize(). - Let the assertion finish the statement.
- Print the total line exactly as the task shows it.
Common mistakes
Counting the wrong endpoint
Pointing a size check at
/users/1still passes with five, becausesize()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 asnameon 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()insidehasSize(), 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.
Related lessons
- hasItem and hasItemsCheck what the array contains, not just how long it is.
- PaginationRead page size, total and result count from a paginated search response.
- Deserialize to ArrayTurn the same user array into a Java array and use its length.
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 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"
}
}