About this lesson
REST Assured given when then: one fluent BDD chain for request, check and extract
Learn REST AssuredBeginnerGetting Started
How it works
REST Assured borrows the given/when/then layout from behaviour-driven development. given() returns a RequestSpecification where you collect what the request needs. when() returns that very same object, so it changes nothing and exists only to make the chain read like a sentence. The HTTP verb method, such as get(), sends the request and returns a Response, and then() turns it into a ValidatableResponse for checks.
These entry points are static methods on RestAssured, which is why the starter adds import static io.restassured.RestAssured.* and you can write a bare given(). After the checks, extract() opens the result again: extract().path() reads one value, and its return type is inferred from the variable you assign it to.
About the assignment
Earlier lessons used separate statements. Here the request for user 1, the status check and the read of the name all happen in one fluent expression, and the value it hands back is stored and printed.
What you will do
- Begin the chain with
given()and lead into the request withwhen(). - Call
get()with/users/1as the path. - Switch to checks with
then()and assert a 200 status withstatusCode(). - Call
extract(), thenpath()with the field name, and assign the result to aString. - Print that variable after the label
User:.
Common mistakes
Assigning the extracted value to the wrong type
path()is generic and trusts your variable type. Assigning thenamefield to anintcompiles, then fails at run time with aClassCastExceptionsaying aStringcannot be cast to anInteger.Thinking when() is required
when()returns the sameRequestSpecification, so leaving it out changes nothing. Keep it for readability, but do not expect it to trigger the request, only the verb method does that.Expecting extract() to run after a failed check
A failing
statusCode()throws anAssertionErroron the spot. The rest of the chain,extract()included, never runs, so the variable is never assigned.
In a real test suite
In a JUnit 5 class each @Test method usually holds one chain like this. Teams add a shared specification with spec() inside given(), and log().ifValidationFails() after then() so a failure prints the full response next to the assertion message.
Related lessons
- Validate 200 OKA chain that exists only to assert the status code.
- Extract Single ValuePull several values out of a validated response with
extract(). - Response Logging on FailureAdd conditional logging to the
then()part of the chain.
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 static io.restassured.RestAssured.*;
public class Main {
public static void main(String[] args) {
RestAssured.baseURI = "https://testkru.com/api";
// TODO: Use given().when().get().then().statusCode().extract().path()
// to get the "name" field from /users/1
// Store the result in a String variable
// Print: "User: <name>"
}
}