Make Selenium Easy

REST Assured Tutorial 8 – BDD Style in Rest Assured

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.

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.

Table of Contents

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

2 thoughts on “REST Assured Tutorial 8 – BDD Style in Rest Assured

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

REST Assured Tutorial 8 – BDD Style in Rest Assured

by Amod Mahajan time to read: 2 min
%d bloggers like this: