About this lesson
REST Assured request and response specification together in one test chain
Learn REST AssuredIntermediateResponse Specification
How it works
Most mature REST Assured suites use both kinds of spec in the same chain: a request spec decides where and how to call, a response spec states what a healthy reply looks like, and the test body adds only what is special about this one case. The result reads almost like a sentence, with given().spec() at the start and then().spec() after the call.
After then().spec() you are still on a ValidatableResponse, so you can keep chaining. Add body() checks with Hamcrest matchers such as equalTo() for fields this test cares about, then switch to extraction with extract(). extract().path() returns the value at a JSON path with a generic return type, so assigning it to a String variable needs no cast.
Order matters for meaning. Every check before extract() runs first, so the value you pull out has already passed the status, content-type and body rules. If any of them fail, the AssertionError is thrown before extraction and your variable is never set.
About the assignment
Create one request spec and one response spec, use both to fetch and validate user 1, and print the extracted name with the confirmation label.
What you will do
- Build a request spec with
RequestSpecBuilderandsetBaseUri(). - Build a response spec with
ResponseSpecBuilder, expecting status 200 andContentType.JSON. - Call
given().spec()with the request spec, GET/users/1, then apply the response spec withthen().spec(). - Add a
body()check on a field such as the id, then useextract().path()to pull outname. - Print the name after the label
Validated and extracted:.
Common mistakes
Extracting before validating
Calling
extract()straight after the request skips the response spec entirely. Putthen().spec()and thebody()checks first so the value is only taken from a reply that passed.Swapping the two specs
A request spec passed to
then().spec(), or a response spec passed togiven().spec(), is a type error. The compiler catches it, but naming variablesreqSpecandresSpecavoids the confusion in the first place.Comparing the id with a string
The id in the JSON is a number.
equalTo()with the text 1 fails because anIntegeris not equal to aString. Match the Java type JsonPath produces.
In a real test suite
This pattern scales well in JUnit 5: specs live in a base class or extension, each @Test method is one readable chain, and values extracted at the end feed the next step of a workflow, such as creating an order for the user you just validated.
Related lessons
- Basic Response SpecificationIntroduces ResponseSpecBuilder on its own.
- Base URI SpecificationIntroduces RequestSpecBuilder with a base URI.
- Extract Single ValueFirst use of extract().path() after validation.
To try one of these calls on any API endpoint without a task, open the Online Selenium Java Compiler (REST Assured is included).
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import io.restassured.http.ContentType;
import io.restassured.specification.ResponseSpecification;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.builder.ResponseSpecBuilder;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
public class Main {
public static void main(String[] args) {
// TODO: Create RequestSpecification with baseUri
// TODO: Create ResponseSpecification with status 200 and ContentType.JSON
// TODO: Use both specs to GET /users/1, validate body, and extract name
// TODO: Print "Validated and extracted: <name>"
}
}