Skip to content
CodeKruPro

hasItem and hasItems

10/62

hasItem and hasItems

Hamcrest provides collection matchers to check if arrays contain specific elements:

  • hasItem("value") — array contains at least this one item
  • hasItems("a", "b") — array contains all of these items

When a JSON array of objects is returned, you reference a field name to get all values of that field:

.body("title", hasItem("The Great Gatsby"))  // on /books: checks every "title" value

Task: GET /users and validate that:

  1. The name array contains "Jane Smith"
  2. The city array contains "New York", "London", and "Paris"

Expected output:

Validated: Jane Smith exists in users
Validated: New York, London, Paris are user cities

About this lesson

REST Assured hasItem and hasItems: Hamcrest collection matchers on JSON arrays

Learn REST AssuredBeginnerResponse Body Validation

How it works

Asking for a field name on an array of objects makes GPath collect that field from every element. On the user list, the path city becomes a Java list of five cities, which is exactly what Hamcrest collection matchers expect. hasItem() passes when at least one element equals the given value, and hasItems() passes when every listed value is present somewhere, in any order and with extra elements allowed.

Stricter matchers exist when order or completeness matters. contains() demands exactly those elements in exactly that order, and containsInAnyOrder() demands exactly those elements in any order, so both fail when given three cities against a list of five. everyItem() applies a matcher to every element, for example checking that each email ends with the same domain.

About the assignment

You check membership rather than position. On the full user list you confirm that one specific person is present and that three named cities all appear, then print two confirmation lines.

What you will do

  1. Send a GET to /users and move to then().
  2. Check the name path with hasItem() and the person the task names.
  3. Add a second body() check on the city path with hasItems() and all three cities.
  4. Print both confirmation lines from the task once the chain has passed.

Common mistakes

  • Using contains() when you mean hasItems()

    contains() sounds like membership but requires the full list in order. With five cities in the response and three in the matcher, it fails with "iterable containing" in the message.

  • Mixing types inside the matcher

    Ages are numbers in this JSON, so hasItem() with the text 35 fails while the number 35 passes. Match the type the JSON really holds.

  • Pointing hasItem at a single value

    On one user, name is a plain string, not a collection, and hasItem() fails with "a collection containing". Use equalTo() for single values.

In a real test suite

Collection matchers are the safest way to check list endpoints whose order is not guaranteed. Negative checks matter too: not(hasItem(...)) confirms that a deleted or filtered record is really gone, which is common after DELETE requests and in search tests.

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:
        //   "name" hasItem "Jane Smith"
        //   "city" hasItems "New York", "London", "Paris"
        // TODO: Print both confirmation messages
    }
}