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
- GET
/users/1with a chain that starts atgiven(). - After
then(), callbody()with the pathnameand theequalTo()matcher holding the expected name. - Let the statement end, there is nothing to extract.
- 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, useequalToIgnoringCase(), 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.
Related lessons
- Validate Multiple FieldsChain several
body()checks on the same user. - Validate Nested JSONUse the same matcher on fields inside nested objects.
- Combined Request and Response SpecsCombine body checks with shared request and response specifications.
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, then validate body("name", equalTo("John Doe"))
// TODO: Print "Validated: name is John Doe"
}
}