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.
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.
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.
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.
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
RecommendedExtract the JWT token cleanly from `/auth/token` and pass it into protected requests with `.auth().oauth2(token)`.
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"));2. Validate Full Contract Using JSON Schema
Catch missing required fields or wrong primitive types automatically using `matchesJsonSchemaInClasspath()`.
given()
.accept(ContentType.JSON)
.when()
.get("/api/users/42")
.then()
.statusCode(200)
.body(matchesJsonSchemaInClasspath("schemas/user-schema.json"));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.
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
// Run full REST Assured 5 challenges interactively at /practice/api-testing/javaBest 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.
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`.