Skip to content
CodeKruPro

Combined Request and Response Specs

43/62

Combined Request and Response Specs

In real frameworks, you combine both request and response specs for clean, DRY tests:

given().spec(reqSpec).get(path).then().spec(resSpec).body(...);

Task: Create both specs and use them together to validate and extract user 1's name.

Expected output:

Validated and extracted: John Doe

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

  1. Build a request spec with RequestSpecBuilder and setBaseUri().
  2. Build a response spec with ResponseSpecBuilder, expecting status 200 and ContentType.JSON.
  3. Call given().spec() with the request spec, GET /users/1, then apply the response spec with then().spec().
  4. Add a body() check on a field such as the id, then use extract().path() to pull out name.
  5. 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. Put then().spec() and the body() 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 to given().spec(), is a type error. The compiler catches it, but naming variables reqSpec and resSpec avoids 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 an Integer is not equal to a String. 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.

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.

Main.java
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>"
    }
}