Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 8 – BDD Style in Rest Assured

Posted on 01/22/2025 By admin

As a part of End to End REST Assured Tutorial , in this post We will learn about “BDD Style in REST Assured”.

BDD is an agile software development process (Not a Software Testing process) which defines expectation from an application to behave from intended user prospective. Each scenario is written in form of Given (Pre-conditions/ Context), When (Action/Event Performed) and Then (Result/Outcomes) format. You can read more about BDD here.

REST Assured allows you to write tests in BDD pattern. Generally we have feature files containing scenarios in BDD for which we write step definitions. This is not the BDD style supported in REST assured. Obviously you can integrate a BDD framework like Cucumber with Rest Assured and achieve BDD pattern which we discussed above.

In fact, Rest Assured allows you to arrange your test script in BDD style using Given, When and Then. Hitting an API has three steps:-

Prerequisites to perform Request:- GIVEN

E.g.- Setting the Base URI, Base Path, Content Type , Request body (Payload) , cookies, headers , authentication , params etc.

Performing Request :- WHEN

E.g:- Which HTTP request to hit i.e. HTTP verbs – GET, POST etc

Verification and extracting response post hitting :- THEN

E.g. Verify status code, response data, log, extracting data etc.

Let’s rewrite the same example discussed in this post, as BDD style:-

package GETExamples;

import org.hamcrest.Matchers;
import org.testng.annotations.Test;

import io.restassured.RestAssured;

public class GetBookingIds_RestfulBookerUsingBDDStyle {

        
        @Test
        public void GetBookingIds_VerifyStatusCode() {
                
                // Given
                RestAssured.given()
                        .baseUri("https://restful-booker.herokuapp.com")
                // When
                .when()
                        .get("/booking")
                // Then
                .then()
                        .statusCode(200)
                        .statusLine("HTTP/1.1 200 OK")
                        // To verify booking count
                        .body("bookingid", Matchers.hasSize(10))
                        // To verify booking id at index 3
                        .body("bookingid[3]", Matchers.equalTo(1));                   
                

        }

}

We have already seen Static import concept in Java. We can implement the same in our example as well.

Let’s import statically below classes in our test class:-

import static io.restassured.RestAssured.*;
import static io.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;

Now we can modify the test by removing explicit call to classes such as RestAssured and Matchers as below:-

package GETExamples;



import static io.restassured.RestAssured.*;
import static io.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;

import org.testng.annotations.Test;


public class GetBookingIds_RestfulBookerUsingBDDStyleAndStaticImport {

        
        @Test
        public void GetBookingIds_VerifyStatusCode() {
                
                // Given
                given()
                        .baseUri("https://restful-booker.herokuapp.com")
                // When
                .when()
                        .get("/booking")
                // Then
                .then()
                        .statusCode(200)
                        .statusLine("HTTP/1.1 200 OK")
                        // To verify booking count
                        .body("bookingid", hasSize(10))
                        // To verify booking id at index 3
                        .body("bookingid[3]", equalTo(1));                    
                

        }

}

Note:- As a beginner, skip using static import as it will confuse you which methods are available in which class and from where it is imported. Once you are well aware of methods of these classes, you can use static import concept concept.

You can clone/download example repo here.

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: Reading Data From CSV in Postman
Next Post: jsonassert tutorials

Related Posts

Are We Using Proper Wait In Proper Way In Selenium Webdriver? Uncategorized
Amod Mahajan, Author at Make Selenium Easy – Page 47 of 47 Uncategorized
Are You Getting NullPointerException For Second TestNG Test While Running As A Suite? Uncategorized
image – Make Selenium Easy Uncategorized
Make Selenium Easy | Make Selenium Easy – Selenium Tutorials : End To End Uncategorized
GIT Tutorials – End To End Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark