Skip to content
CodeKruPro

Write a REST Assured test in given/when/then style

Automation taskEasyJava onlyAsked in: Capgemini, Coforge, Wipro

Many API rounds start with: "Write a simple REST Assured test for GET /users/1." Practise exactly that here: build the request, send it and check the status and body, with real grading.

BDD Style - given/when/then

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