Rest Assured cheatsheet
A one-page reference for Rest Assured. For POJO serialization and advanced filters, see the complete guide.
๐ Full guide: Rest Assured โgiven/when/then DSLโ
given()
.baseUri("https://api.example.com")
.header("Content-Type", "application/json")
.when()
.get("/users/1")
.then()
.statusCode(200)
.body("name", equalTo("Abhishek"));
Request specificationsโ
RequestSpecification spec = new RequestSpecBuilder()
.setBaseUri("https://api.example.com")
.addHeader("Authorization", "Bearer " + token)
.build();
given().spec(spec).when().get("/orders").then().statusCode(200);
Reuse one spec across a whole test class โ no repeated boilerplate.
JSON path & Hamcrestโ
.then()
.body("data.size()", equalTo(3))
.body("data[0].id", notNullValue())
.body("data.name", hasItem("Widget"));
Authenticationโ
given().auth().oauth2(token)...
given().auth().basic("user", "pass")...
given().auth().preemptive().basic("user", "pass")...
Serialization with POJOsโ
User user = new User("Abhishek", 30);
given().contentType(ContentType.JSON).body(user)
.when().post("/users")
.then().statusCode(201)
.extract().as(User.class);
JSON Schema validationโ
.then().body(matchesJsonSchemaInClasspath("user-schema.json"));
Catches structural contract breaks (missing/renamed fields), not just value assertions.
Logging & debuggingโ
given().log().all() // log request
.when().get("/users")
.then().log().ifError(); // log response only on failure
Rest Assured vs Postmanโ
| Rest Assured | Postman | |
|---|---|---|
| Fits into | Java test suite, CI | standalone GUI tool |
| Diffs/review | clean, code-based | noisy JSON export |