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:-

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:-

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

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

You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply