Skip to content
CodeKruPro

Validate a Single Field

7/62

Validate a Single Field

REST Assured uses Hamcrest matchers to validate response fields directly in the .then() block:

given().get("/books/1")
    .then()
    .body("title", equalTo(expectedTitle));

The first argument is the JSON field to read and the second is the matcher it must satisfy. If the value differs, the request throws an AssertionError that shows both values.

The equalTo() matcher comes from import static org.hamcrest.Matchers.*;

Task:

  1. GET /users/1
  2. Validate that the name field equals "John Doe" using equalTo()
  3. Print the confirmation

Expected output:

Validated: name is John Doe

About this lesson

REST Assured body equalTo assertion on a single JSON field

Learn REST AssuredBeginnerResponse Body Validation

How it works

Inside then(), the body() method takes two arguments: a path into the JSON response and a Hamcrest matcher that the value at that path must satisfy. equalTo() from org.hamcrest.Matchers is the strictest of them: the value must be equal in both content and Java type.

When the match fails, REST Assured throws an AssertionError whose message names the path, then shows the expected and actual values on separate lines under a "JSON path name doesn't match." header. That message is often all you need to see what went wrong, because it shows what the server really returned.

About the assignment

Instead of reading the name and comparing it yourself, you ask REST Assured to verify it. The request for user 1 and the check that its name is John Doe form one chain, and a confirmation line follows only if the check holds.

What you will do

  1. GET /users/1 with a chain that starts at given().
  2. After then(), call body() with the path name and the equalTo() matcher holding the expected name.
  3. Let the statement end, there is nothing to extract.
  4. Print the confirmation message from the task on the next line.

Common mistakes

  • A typo in the path compares against null

    A path that matches nothing yields null, and the failure reads "Actual: null". The assertion still fails, but the message points at the path rather than the data, so check the spelling first.

  • Case or whitespace differences

    equalTo() is exact. If the API may change capitalisation, use equalToIgnoringCase(), which passes for "john doe" against the stored value.

  • Checking text in a number field

    equalTo() also compares types, so a quoted number never matches a JSON number. The next lesson shows this with the user's age.

In a real test suite

is() is a readability wrapper around equalTo(), so a check like body("name", is(...)) behaves the same. Most suites mix such exact checks with looser matchers like containsString() or notNullValue() for values that change, such as generated ids and timestamps.

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, then validate body("name", equalTo("John Doe"))
        // TODO: Print "Validated: name is John Doe"
    }
}