Make Selenium Easy

REST Assured Tutorial 14 –RequestSpecification – How the request will look like

Introduction

As a part of End to End REST Assured Tutorial, in this post, we will learn about RequestSpecification in Rest Assured. We will learn what, when, and how to use RequestSpecification in Rest Assured.

Prerequisite

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

Why RequestSpecification?

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

Above we have only two tests but in real-time you may have many. A group of tests will have some common specifications to create a request. Following the DRY principle is the best way to write clean code. We have repeated common request specifications 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 request specifications together and put as a common entity, we can use RequestSpecification in Rest Assured. RequestSpecification is an interface that allows you to specify how the request will look like. This interface has readymade methods to define base URL, base path, headers, etc. We need to use given() method of RestAssured class to get a reference for RequestSpecification. Remember RequestSpecification is an interface and we can not create an object of it. RequestSpecificationImpl is its implemented class.

How to use RequestSpecification?

A RequestSpecification with some specifications can be created as below:-

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

We can add a RequestSpecification to a request in multiple ways as shown below:-

Example Program

There are two methods in RestAssured class to start creating Request specifications:-

  1. given()
  2. with()

The return type of both methods is RequestSpecification. There is no difference between the above two methods. The only difference is syntactical. An example code is below:-

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

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

1 thought on “REST Assured Tutorial 14 –RequestSpecification – How the request will look like

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.