Skip to content
CodeKruPro

Validate Multiple Fields

8/62

Validate Multiple Fields

You can chain multiple .body() assertions in a single request:

.then()
    .body("id", equalTo(1))
    .body("title", equalTo(expectedTitle))
    .body("rating", equalTo(expectedRating));

Each .body() line checks one field. Whole numbers in the JSON come back as Integer, so compare them with an int such as 1, not the String "1".

If any assertion fails, REST Assured throws an error immediately.

Task: GET /users/1 and validate all four fields: name, email, age, and city.

Expected output:

All fields validated for user John Doe

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

  1. Request /users/1 and move to then().
  2. Add a body() check with equalTo() for name and another for email.
  3. Check age with an int, since the JSON stores it as a number.
  4. Check city, then end the chain and print the confirmation line from the task.

Common mistakes

  • Quoting the age

    Comparing age with 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 a String and the other an Integer.

  • 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 single body() 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.

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/1 and validate:
        //   name = "John Doe"
        //   email = "john@example.com"
        //   age = 30
        //   city = "New York"
        // TODO: Print confirmation
    }
}