Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 53 – How to create ResponseSpecification using ResponseSpecBuilder

Posted on 02/19/2025 By admin

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.

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

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();
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

Uncategorized

Post navigation

Previous Post: Postman Tutorial Part 15 – Adding Automation Test Scripts In Postman
Next Post: REST Assured Tutorial 5 – Abstraction – Hide The Implementation

Related Posts

March 2, 2019 – Make Selenium Easy Uncategorized
Advanced Selenium Concepts – Custom Select Class – Select Value Containing Specific Text in Drop down | Make Selenium Easy Uncategorized
REST Assured Tutorial 72 – How To Compare Part of JSON Objects and Arrays using JSONassert library Uncategorized
October 3, 2017 – Make Selenium Easy Uncategorized
Handling Frames/IFrames In Selenium WebDriver : Part 2 Uncategorized
API Testing Tutorial Part 14 – Sending First GET Request in Postman – Make Selenium Easy 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