Skip to content
CodeKruPro

BDD Style - given/when/then

3/62

BDD Style - given/when/then

REST Assured supports a BDD (Behavior-Driven Development) style syntax that reads like English:

given()  // preconditions (headers, params, body)
    .when()  // the action
    .get("/books/1")  // HTTP method and endpoint
    .then()  // assertions
    .statusCode(200)  // validate status
    .extract()  // extract data
    .path("title");  // get a field

To use this style, add the static import: import static io.restassured.RestAssured.*;

Task:

  1. Use the BDD chain to GET /users/1
  2. Validate the status code is 200
  3. Extract the name field
  4. Print it

Expected output:

User: John Doe

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

  1. Begin the chain with given() and lead into the request with when().
  2. Call get() with /users/1 as the path.
  3. Switch to checks with then() and assert a 200 status with statusCode().
  4. Call extract(), then path() with the field name, and assign the result to a String.
  5. 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 the name field to an int compiles, then fails at run time with a ClassCastException saying a String cannot be cast to an Integer.

  • Thinking when() is required

    when() returns the same RequestSpecification, 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 an AssertionError on 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.

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 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>"
    }
}