Make Selenium Easy

REST Assured Tutorial 16 – Building RequestSpecification Using RequestSpecBuilder

Introduction

As a part of the End to End REST Assured Tutorial, in this post, we will learn about class RequestSpecBuilder and how it can be used to create RequestSpecification in Rest Assured.

Prerequisite post

RequestSpecification – How The Request Will Look Like

Multiple Ways Of Calling HTTP Methods On A RequestSpecification

Class RequestSpecBuilder

There is another way of creating RequestSpecification in Rest Assured and that is by using class RequestSpecBuilder.

RequestSpecBuilder is a class in Rest Assured, which contains methods to set cookies, headers, multipart details, body, authentication, form parameters, query parameters, path parameters, base path, base URI, proxy, etc. These all are required to construct a Requestspecification. After adding all required details, we need to use “build()” method of RequestSpecBuilder class to get a RequestSpecification reference.

RequestSpecification helps us to reuse specifications.

Example Program

Output

Note:- You might get an exception “java.lang.NullPointerException: Cannot get property ‘assertionClosure’ on null object” on running above program. There is an existing bug in Rest Assured which is in an open state as of now. You can comment line no 23 and 24 “Response res1= reqSpec.get();” to run program successfully.

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

9 thoughts on “REST Assured Tutorial 16 – Building RequestSpecification Using RequestSpecBuilder

  1. Seems that there is one small issue on line number 23. I believe it should be something like this->Response res1 = RestAssured.given(reqSpec).get();
    ..Thank you so much Amod Mahajan for these tutorials.

  2. is there any time we will be using this because we can directly use RequestSpecifiation as its increasing syntax not helping anyways?

    Any use case for this?

    Will that help separating out the request related data..
    so we can have multiple things like this

    RequestSpecification req1 = ReqSpec1.build()
    RequestSpecification req2 = ReqSpec2.build()
    RequestSpecification req3 = ReqSpec3.build()
    RequestSpecification req4 = ReqSpec4.build()

    let me know if any way this will be useful

    1. Hi Gaurav,
      is this code working for you.When I am trying to use RequestSpecBuilder, I am getting below error:-

      java.lang.NullPointerException: Cannot get property ‘assertionClosure’ on null object

      Any idea regarding this?

    2. ResponseSpecBuilder and RequestSpecBuilder are just another way of creating ResponseSpecification and RequestSepcification respectively. ResponseSpecBuilder and RequestSpecBuilder contain more meaningful methods by name. Yes as above you can have multiple specifications and reuse them. You can also put common specifications together and use them in your request. You may have multiple specs in a request.

  3. I have referred lots of sites, bolgs and you-tube video’s but I always gets confused. Thank you so much Amod for this systematic and very explanative tutorials, now I’m getting confidence in APi testing using Rest assured. Keep it up…….

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 16 – Building RequestSpecification Using RequestSpecBuilder

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