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
- Send a GET to
/usersand move tothen(). - Check the
namepath withhasItem()and the person the task names. - Add a second
body()check on thecitypath withhasItems()and all three cities. - 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,
nameis a plain string, not a collection, andhasItem()fails with "a collection containing". UseequalTo()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.
Related lessons
- Validate Array SizeCount the same array before checking what it holds.
- Extract List of ValuesExtract the collected names and cities as Java lists instead of matching them.
- Filter by CategoryRead the names from a category-filtered search as a list.
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:
// "name" hasItem "Jane Smith"
// "city" hasItems "New York", "London", "Paris"
// TODO: Print both confirmation messages
}
}