About this lesson
REST Assured: validate multiple JSON fields in one response
Learn REST AssuredBeginnerResponse Body Validation
How it works
Each call to body() on a ValidatableResponse returns that response again, so several field checks can follow one another after a single then(). REST Assured evaluates them eagerly: each body() call is checked as soon as it runs, and the first failing one throws, so later calls in the chain are never evaluated.
There is a second form. One body() call can take several path and matcher pairs in a row. Those pairs are evaluated together, and the error reports every mismatch at once, for example "2 expectations failed." followed by one block per field. That is handy when you want the full picture from one run.
Types matter for every pair. JSON whole numbers arrive as Integer, strings as String, and true or false as Boolean, and equalTo() compares the type as well as the value.
About the assignment
User 1 has four data fields worth checking: name, email, age and city. You verify all four in a single request, then print one line saying the user passed, which only appears if every check held.
What you will do
- Request
/users/1and move tothen(). - Add a
body()check withequalTo()fornameand another foremail. - Check
agewith anint, since the JSON stores it as a number. - Check
city, then end the chain and print the confirmation line from the task.
Common mistakes
Quoting the age
Comparing
agewith the text 30 fails. The message hints at the cause: the expected value shows bare while the actual value appears in angle brackets, because one is aStringand the other anInteger.Expecting all chained failures at once
With one
body()per field, only the first failing field is reported. If you need every mismatch in one run, pass all pairs to a singlebody()call.Copying the email by hand
Typing the expected email from memory is a common source of false failures. Copy expected values from the API documentation or a real response.
In a real test suite
Checking many fields one by one gets long. Real suites switch to as() and a Java class when they need most of the record, or to a JSON Schema when they care about structure and types rather than exact values, and keep body() chains for the few fields a test is really about.
Related lessons
- Validate a Single FieldThe single-field version with
equalTo()and its failure message. - Deserialize Response to POJOMap the whole user onto a Java class and check its fields there.
- User Schema ValidationValidate the structure and types of user 1 against a JSON Schema.
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/1 and validate:
// name = "John Doe"
// email = "john@example.com"
// age = 30
// city = "New York"
// TODO: Print confirmation
}
}