Make Selenium Easy

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

Introduction

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.

Prerequisite

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

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

Why ResponseSpecification?

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

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.

How to use ResponseSpecification?

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

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

Example Program

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.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

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 52 – ResponseSpecification – Specify how the expected response must look like

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