Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 52 – ResponseSpecification – Specify how the expected response must look like

Posted on 11/11/2024 By admin

As a part of End to End REST Assured Tutorial, in this post, we will learn what is ResponseSpecification in Rest Assured and when and how to use it.

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

  io.rest-assured rest-assured 4.3.1 test

I have covered RequestSpecification earlier which you must go through once.

Observe the below lines of codes carefully. You will find that we have some common statements in both @Test methods after then() method call.

package ResponseSpecificationExample;

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

import io.restassured.RestAssured;
import io.restassured.http.ContentType;

public class WithoutUsingResponseSpecification {
        
        @Test
        public void getAllBookings()
        {
                // Given
                RestAssured
                  .given()
                         .baseUri("https://restful-booker.herokuapp.com")
                // When
                   .when()
                          .get("/booking")
                // Then
                   .then()
                   .contentType(ContentType.JSON)
                   .time(Matchers.lessThan(5000L))
                   .statusLine("HTTP/1.1 200 OK")
                // 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=Rahul")
                // Then
                   .then()
                // Repetitive validation as first test above
                   .contentType(ContentType.JSON)
                   .time(Matchers.lessThan(5000L))
                   .statusLine("HTTP/1.1 200 OK")
                // To verify booking count
                   .body("size()", Matchers.equalTo(0));
                        
        }

}

Above we have only two tests but in real-time you may have many. A group of tests will have common assertions on response. Following the DRY principle is the best way to write clean code. We have repeated common assertions in both tests which is not a good practice. If we need to modify then we need to make changes every placed used.

To club common assertions together and put as a common entity, we can use ResponseSpecification in Rest Assured. ResponseSpecification is an interface that allows you to specify how the expected response must look like in order for a test to pass. This interface has readymade methods to define assertions like status code, content type, etc. We need to use expect() method of RestAssured class to get a reference for ResponseSpecification. Remember ResponseSpecification is an interface and we can not create an object of it.

A ResponseSpecification can be created for some assertions as below:-

// Create a ResponseSpecification 
responseSpecification=  RestAssured.expect();
responseSpecification.contentType(ContentType.JSON);
responseSpecification.statusCode(200);
responseSpecification.time(Matchers.lessThan(5000L));
responseSpecification.statusLine("HTTP/1.1 200 OK");

We can add a ResponseSpecification using spec() method as below:-

...
.then()
.spec(responseSpecification)
package ResponseSpecificationExample;

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

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

public class UsingResponseSpecification {
        
        ResponseSpecification responseSpecification = null;
        
        @BeforeClass
        public void setupResponseSpecification()
        {
                // Create a ResponseSpecification 
                responseSpecification=  RestAssured.expect();
                responseSpecification.contentType(ContentType.JSON);
                responseSpecification.statusCode(200);
                responseSpecification.time(Matchers.lessThan(5000L));
                responseSpecification.statusLine("HTTP/1.1 200 OK");
                
        }
        
        @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: Manual Testing
Next Post: oauth2.o flow

Related Posts

Advanced Concept of Selenium – Design Patterns in Selenium WebDriver – There is Not Only Page Object Model Design Pattern Uncategorized
Introduction To Apache POI – The Java API To Read Write Microsoft Documents – Excel Uncategorized
October 29, 2017 – Make Selenium Easy Uncategorized
Handling Website Popups In Selenium webdriver | Make Selenium Easy Uncategorized
browser Uncategorized
chrome 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