REST Assured Tutorial 53 – How to create ResponseSpecification using ResponseSpecBuilder

Introduction

As a part of End to End REST Assured Tutorial, in this post, we will learn to create a ResponseSpecification using ResponseSpecBuilder class in Rest Assured.

Prerequisite

I have used below dependency of Rest Assured library for this post:-



    io.rest-assured
    rest-assured
    4.3.1
    test

You should go through below posts once to understand this post better:-

Interface RequestSpecification – How The Request Will Look Like

ResponseSpecification – Specify How The Expected Response Must Look Like

What is ResponseSpecBuilder?

ResponseSpecBuilder class provides you a builder to create a ResponseSpecification. This class has self explanatory methods like expectStatusCode(), expectHeaders() etc compare to methods of ResponseSpecification interface.

We can create a ResponseSpecification using ResponseSpecBuilder as below:-

// Create a ResponseSpecification using ResponseSpecBuilder
ResponseSpecBuilder responseSpecBuilder = new ResponseSpecBuilder();
responseSpecBuilder.expectStatusCode(200);
responseSpecBuilder.expectStatusLine("HTTP/1.1 200 OK");
responseSpecBuilder.expectContentType(ContentType.JSON);
responseSpecBuilder.expectResponseTime(Matchers.lessThan(5000L));	
ResponseSpecification responseSpecification = responseSpecBuilder.build();

Or instead of calling ResponseSpecBuilder reference multiple times, we can use the builder pattern as below:-

// Create a ResponseSpecification using ResponseSpecBuilder
responseSpecification = new ResponseSpecBuilder()
	.expectStatusCode(200)
     .expectStatusLine("HTTP/1.1 200 OK")
	.expectContentType(ContentType.JSON)
	.expectResponseTime(Matchers.lessThan(5000L))	
	.build();

Example Program

package ResponseSpecificationExample;

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

import io.restassured.RestAssured;
import io.restassured.builder.ResponseSpecBuilder;
import io.restassured.http.ContentType;
import io.restassured.specification.ResponseSpecification;

public class ResponseSpecBuilderExampleBuilderPattern {
	
ResponseSpecification responseSpecification = null;
	
	@BeforeClass
	public void setupResponseSpecification()
	{
		// Create a ResponseSpecification using ResponseSpecBuilder
		responseSpecification = new ResponseSpecBuilder()
			.expectStatusCode(200)
		    .expectStatusLine("HTTP/1.1 200 OK")
		    .expectContentType(ContentType.JSON)
		    .expectResponseTime(Matchers.lessThan(5000L))	
		    .build();
	}
	
	@Test
	public void getAllBookings()
	{
		// Given
		RestAssured
		  .given()
			 .baseUri("https://restful-booker.herokuapp.com")
		// When
		   .when()
			  .get("/booking")
		// Then
		   .then()
		   // Just pass ResponseSpecification as below
		   .spec(responseSpecification)
		// To verify booking count
		   .body("size()", Matchers.greaterThan(5));
		
	}
	
	@Test
	public void getBookingDetailsWithInvalidFirstName()
	{
		// Given
		RestAssured
		  .given()
			 .baseUri("https://restful-booker.herokuapp.com")
		// When
		   .when()
			  .get("/booking?firstname=jim")
		// Then
		   .then()
		   .spec(responseSpecification)
		// To verify booking count
		   .body("size()", Matchers.equalTo(0));
			
	}


}

You can download/clone the above sample project from here.

You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.

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

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

2 thoughts on “REST Assured Tutorial 53 – How to create ResponseSpecification using ResponseSpecBuilder”

  1. responseSpecBuilder.expectResponseTime(Matchers.lessThan(1000L));
    here what is the meaning of “L”

Leave a Reply

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