Skip to content
Target QATargetQA
REST Assured (Java) · JavaFile Operations & NetworkIntermediate5 min read

REST Assured Java API Testing Guide: Bearer Auth, JSONPath & Schema Validation

A practical SDET reference for REST Assured 5 in Java: chaining login tokens across requests, asserting nested JSON arrays with Hamcrest matchers, and validating response contracts against JSON Schema.

By TargetQA Engineering Team•Updated 2026-11-20
Interactive Cloud Sandbox

Test this solution live in real cloud browsers

Preload the working test script directly into TargetQA's REST Assured (Java) compiler. Zero local dependencies or browser drivers needed.

Test Live in Compiler
The Problem & Error Symptoms

Understanding the Breakdown

SDET technical interviews almost always test API automation: authenticating to obtain a JWT token, passing it in an Authorization header, asserting nested JSON payloads, and catching breaking contract changes.

Under the Hood

Why Traditional Selectors Fail

Parsing response strings manually with regex or checking only `statusCode(200)` misses broken response fields, wrong data types, and authorization leaks.

Field-Tested Patterns

Production-Grade Solutions

Choose the strategy that matches your test architecture. Pattern #1 is recommended for most production test suites.

1. Authenticate & Chain a Bearer Token in REST Assured

Recommended

Extract the JWT token cleanly from `/auth/token` and pass it into protected requests with `.auth().oauth2(token)`.

1. Authenticate & Chain a Bearer Token in REST Assured (java)
String token = given()
  .contentType(ContentType.JSON)
  .body("{\"email\":\"qa@targetqa.com\",\"password\":\"Secret123!\"}")
.when()
  .post("/api/auth/token")
.then()
  .statusCode(200)
  .extract().path("token");

given()
  .auth().oauth2(token)
.when()
  .get("/api/orders")
.then()
  .statusCode(200)
  .body("orders.size()", greaterThan(0))
  .body("orders[0].status", equalTo("CONFIRMED"));
How it works: REST Assured's BDD syntax chains extraction and assertions cleanly without boilerplate HTTP client code.

2. Validate Full Contract Using JSON Schema

Catch missing required fields or wrong primitive types automatically using `matchesJsonSchemaInClasspath()`.

2. Validate Full Contract Using JSON Schema (java)
given()
  .accept(ContentType.JSON)
.when()
  .get("/api/users/42")
.then()
  .statusCode(200)
  .body(matchesJsonSchemaInClasspath("schemas/user-schema.json"));
How it works: Schema validation checks every required key, nested object, and field type in one assertion.
Full Executable Cloud Test
Open in compiler

Complete Working Test Script

You can copy this exact test into your repository or run it immediately in our cloud sandbox against live demo targets.

test-recipe.py
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

// Run full REST Assured 5 challenges interactively at /practice/api-testing/java
Target framework: REST Assured (Java) · Language: Java
Preload & Run in Compiler
Production Checklist

Best Practices & Golden Rules

  • Always assert status code, `Content-Type` header, AND key response body fields.
  • Write negative API tests for 400 Bad Request, 401 Unauthorized, 403 Forbidden, and 404 Not Found.
  • Use `RequestSpecification` builders to avoid repeating base URI, headers, and logging config.
Avoid Test Flakiness

Common Pitfalls to Avoid

  • Only checking `statusCode(200)` without verifying the payload body or schema contract.
  • Hardcoding expired static tokens instead of obtaining fresh tokens in `@BeforeAll`.

Frequently Asked Questions

Can I practice REST Assured online without installing Java or Maven?

Yes. TargetQA provides 64+ zero-setup REST Assured Java API testing challenges at `/practice/api-testing/java` against realistic live mock endpoints.

What Hamcrest matchers are most important for SDET interviews?

Focus on `equalTo`, `notNullValue`, `hasItem`, `hasItems`, `everyItem`, `greaterThan`, `containsString`, and `hasSize`.

Hands-On Practice

Practice This Skill in TargetQA Challenges

Explore More Automation Guides