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

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

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.

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

Leave a Reply

Your email address will not be published. Required fields are marked *