Advanced Assertions
In REST Assured, advanced assertions help you validate complex responses in a more detailed and flexible way. You can check not only basic things like status codes but also deeply nested fields, lists, dynamic values, and more.
In this tutorial, you will learn:
- Using Hamcrest Matchers for advanced validations.
- Asserting complex JSON responses (arrays, nested objects).
- Asserting XML responses with XPath-like checks.
- Validating dynamic values and patterns (like regular expressions)
- Combining multiple assertions for stronger tests.
Using Hamcrest Matchers for Advanced Assertions
REST Assured uses Hamcrest matchers to write flexible and readable assertions.
Matcher | Description |
---|---|
equalTo(value) | Exact match. |
not(equalTo(value)) | Opposite of equalTo(). |
containsString(value) | Partial match for substrings. |
startsWith(value) | Checks if the value starts with a prefix. |
endsWith(value) | Checks if the value ends with a suffix. |
hasItem(item) | Checks if a list contains a value. |
hasItems(item1, item2) | Checks if a list contains multiple values. |
hasSize(n) | Checks the size of a list or array. |
everyItem(condition) | Validates all elements in a list. |
greaterThan(value) | Checks if a number is greater. |
lessThan(value) | Checks if a number is smaller. |
notNullValue() | Ensures the value is not null. |
Example 1: Validate Multiple Fields
given() .when() .get("http://192.xxx.xx.xxx:8080/books/20250401") .then() .body("name", equalTo("Book 1")) .body("language", notNullValue()) .body("publisher", containsString("ABC"));
Example 2: Validate Lists in JSON
given() .when() .get("http://192.xxx.xx.xxx:8080/books") .then() .body("name", hasItem("Book 1")) // Check if list contains item .body("size()", greaterThan(5)); // Ensure list has more than 5 items
Advanced JSON Assertions
Modern APIs often return complex JSON with arrays and nested objects. REST Assured makes it easy to validate them.
Sample JSON Response:
{ "id": 1, "name": "Leanne Graham", "address": { "city": "Gwenborough", "zipcode": "92998-3874" }, "phoneNumbers": ["123-456-7890", "987-654-3210"] }
Validate Nested JSON Fields
given() .when() .get("http://192.xxx.xx.xxx:8080/users/1") .then() .body("address.city", equalTo("Gwenborough")) .body("address.zipcode", startsWith("92998"));
Validate JSON Arrays
given() .when() .get("http://192.xxx.xx.xxx:8080/users/1") .then() .body("phoneNumbers", hasSize(2)) // Array has 2 items .body("phoneNumbers", hasItem("123-456-7890"));
Validate Every Item in an Array
Check if all items in an array meet a condition.
given() .when() .get("http://192.xxx.xx.xxx:8080/users") .then() .body("address.zipcode", everyItem(startsWith("9")));
Advanced XML Assertions
If the API returns XML, REST Assured allows you to perform advanced checks using XmlPath.
Sample XML Response:
<user> <id>1</id> <name>John Doe</name> <address> <city>New York</city> </address> </user>
Validate XML Elements
given() .accept("application/xml") .when() .get("http://192.xxx.xx.xxx:8080/users/1") .then() .body("user.name", equalTo("John Doe")) .body("user.address.city", equalTo("New York"));
Validating Dynamic Values
Sometimes API responses contain dynamic values (e.g., timestamps, IDs).
Ignore Dynamic Fields
You can ignore fields that change.
given() .when() .get("http://192.xxx.xx.xxx:8080/books/20250401") .then() .body("isbn", notNullValue()) // ISBN can be dynamic but not null .body("price", greaterThan(0));
Use Regular Expressions
Use matchesPattern() for complex string matching.
given() .when() .get("http://192.xxx.xx.xxx:8080/books/20250401") .then() .body("createdAt", matchesPattern("\\d{4}-\\d{2}-\\d{2}"));
This checks if createdAt is in YYYY-MM-DD format.
Combining Multiple Assertions
You can combine many checks in a single request.
Complex Assertions
given() .when() .get("http://192.xxx.xx.xxx:8080/users/1") .then() .statusCode(200) .body("name", equalTo("Leanne Graham")) .body("address.city", equalTo("Gwenborough")) .body("phoneNumbers", hasItem("123-456-7890")) .body("phoneNumbers", hasSize(2)) .header("Content-Type", containsString("json"));
Assert Response Time
You can check if the API responds within a specific time.
given() .when() .get("http://192.xxx.xx.xxx:8080/books") .then() .time(lessThan(2000L)); // Response time < 2 seconds