assert_
glossary ↓ downloads
track: java · selenium
suite 06 · api automation

Your first API test — REST Assured

API automation15 min
as a manual tester

Manually: send request in Postman, eyeball the JSON, judge pass/fail.

in automation

REST Assured writes that as given/when/then — and its syntax literally reads like a test case.

REST Assured is Java's standard API-testing library. Add it to pom.xml — and note the official quirk: declare it before the JUnit dependency so the right Hamcrest version wins:

pom.xml — before the junit-jupiter dependency
<dependency>
  <groupId>io.rest-assured</groupId>
  <artifactId>rest-assured</artifactId>
  <version>6.0.0</version>
  <scope>test</scope>
</dependency>
UserApiTest.java
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

public class UserApiTest {

    @Test
    void userOneIsLeanne() {
        given()
            .baseUri("https://jsonplaceholder.typicode.com")
        .when()
            .get("/users/1")
        .then()
            .statusCode(200)
            .body("name", equalTo("Leanne Graham"))
            .body("address.city", equalTo("Gwenborough"))   // the dot path!
            .body("company.name", containsString("Romaguera"));
    }
}
predict: what does this print? think it through, then reveal
output
[INFO] Running UserApiTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] Time elapsed: 1.2 s          ← no browser. feel the speed.
[INFO] BUILD SUCCESS

given (setup) / when (the action) / then (assertions) — preconditions, steps, expected results. Your test case template became a syntax.

Matchers — assertion vocabulary

the Hamcrest matchers you'll reach for
.body("name", equalTo("Leanne Graham"))
.body("email", containsString("@"))
.body("id", greaterThan(0))
.body("$", hasSize(10))                // $ = whole response; size of an array
.body("title", notNullValue())

When an API test fails

output — a failing body() assertion
java.lang.AssertionError: 1 expectation failed.
JSON path name doesn't match.
Expected: Leanne Graham
  Actual: Ervin Howell

Expected vs actual, with the JSON path named — the same failure anatomy you've read since Suite 01, minus screenshots because there's nothing to photograph. For debugging, add .log().all() after then() to print the entire request and response.

⚠ when it breaks — the classics
Matchers crash with a NoSuchMethodError
java.lang.NoSuchMethodError: 'org.hamcrest.Matcher org.hamcrest.Matchers.equalTo(…)'

Fix: The classic ordering bug: rest-assured must be declared before junit-jupiter in pom.xml so the right Hamcrest wins. Reorder, reload Maven.

Status is 404 but the endpoint 'looks right'
java.lang.AssertionError: 1 expectation failed. Expected status code <200> but was <404>.

Fix: Usually a missing leading slash or a pluralization typo: get("users/1") vs get("/users/1"), /user/1 vs /users/1. Add .log().all() after then() to see the exact URL that was called.

⚡ exercise · test the comments endpoint

Write CommentsApiTest against /posts/1/comments:

  1. Status is 200.
  2. The array has exactly 5 comments ("$", hasSize(5)).
  3. The first comment's email ("[0].email") contains "@".
  4. Negative test: GET /posts/99999 returns 404. Your first API boundary test.
key takeaways
  • given/when/then = preconditions/steps/expected — your template as syntax.
  • body("a.b.c", matcher) asserts on any JSON path; $ means the whole response.
  • API tests run in milliseconds with zero flakiness sources — automate deep here.
  • Declare rest-assured before junit in pom.xml (Hamcrest versioning).
← REST in 15 minutes Chaining requests — real API flows →