What is REST Assured?
REST Assured is an open-source, Apache 2.0 licensed Java library for testing HTTP APIs, created and maintained by Johan Haleby with other contributors. It brings the readability of dynamic-language HTTP testing to the JVM: a test reads as given() (the request), when() (the call) and then() (the assertions), and plugs into JUnit or TestNG like any other test. Response bodies are queried with Groovy-style JsonPath and XmlPath expressions and checked with Hamcrest matchers, and optional modules add JSON Schema validation and Spring MockMvc support.
Why use it in the API Lifecycle?
- Fluent request and assertion DSL:
given().header(...).body(...).when().post("/orders").then().statusCode(201)keeps request setup and expectations in one readable chain. - Path-based assertions: JsonPath and XmlPath expressions such as
body("items.size()", is(2))assert on specific fields without deserializing the whole response. - JSON Schema validation: The
json-schema-validatormodule checks a response withmatchesJsonSchemaInClasspath("products-schema.json"), and 6.1 adds support for JSON Schema draft-06, draft-07, 2019-09 and 2020-12. - Reusable specifications:
RequestSpecBuilderandResponseSpecBuildercapture shared base URIs, headers, auth and common expectations once for a whole suite. - Value extraction:
extract().path("_links.next.href")pulls a value out of a validated response for the next request in a flow. - Spring integration: The
spring-mock-mvcmodule runs the same DSL against a Spring MVC application without starting a server, and the Spring modules target Spring 7 since 6.0.
Best Practices
- Define a
RequestSpecificationper API (base URI,Accept, auth) and aResponseSpecificationfor invariants such as theContent-Type, so individual tests state only what is specific to them. - Validate responses against the JSON Schema generated from, or embedded in, your OpenAPI document with
matchesJsonSchemaInClasspath, in addition to asserting the few fields the test is about. One catches drift, the other catches wrong values. - Turn on
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails()globally. Full request and response logs on failure make CI failures diagnosable, and logging only on failure keeps passing runs quiet and avoids writing tokens to logs needlessly. - Use the MockMvc module for fast controller-level tests and a real HTTP server for a smaller set of end-to-end tests. MockMvc skips the servlet container, so it will not catch filters, proxies or serialization settings that only apply on the wire.
- Assert error responses as carefully as success: status,
Content-Type: application/problem+json, and thetypeandstatusmembers. The broader layering of unit, contract and end-to-end tests is in API Testing. - Do not chain a whole user journey into one test method. Extract values between steps where you must, but keep each test small enough that its name explains its failure.