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

package RestAssuredConcepts;

import io.restassured.RestAssured;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

public class RequestSpecBuilderExample {

	public static void main(String[] args) {

		// Creating an object of RequestSpecBuilder
		RequestSpecBuilder reqBuilder = new RequestSpecBuilder();
		// Setting Base URI
		reqBuilder.setBaseUri("https://restful-booker.herokuapp.com");
		// Setting Base Path
		reqBuilder.setBasePath("/booking");
		// Getting RequestSpecification reference using builder() method
		RequestSpecification reqSpec = reqBuilder.build();
		
		// Usage in different styles
		// We can directly call http verbs on RequestSpecification
		Response res1= reqSpec.get();
		System.out.println(res1.asString());
		System.out.println("======================");
		
		// We can also pass RequestSpecification reference variable in overloaded given() method
		Response res2 = RestAssured.given(reqSpec).get();
		System.out.println(res2.asString());
		System.out.println("======================");
				
		// We can also pass RequestSpecification using spec() method
		Response res3 = RestAssured.given().spec(reqSpec).get();
		System.out.println(res3.asString());

	}
}

Output

[{"bookingid":11},{"bookingid":13},{"bookingid":15},{"bookingid":1},{"bookingid":6},{"bookingid":2},{"bookingid":8},{"bookingid":4},{"bookingid":5},{"bookingid":3},{"bookingid":10},{"bookingid":12},{"bookingid":7},{"bookingid":14},{"bookingid":9}]
======================
[{"bookingid":11},{"bookingid":13},{"bookingid":15},{"bookingid":1},{"bookingid":6},{"bookingid":2},{"bookingid":8},{"bookingid":4},{"bookingid":5},{"bookingid":3},{"bookingid":10},{"bookingid":12},{"bookingid":7},{"bookingid":14},{"bookingid":9}]
======================
[{"bookingid":11},{"bookingid":13},{"bookingid":15},{"bookingid":1},{"bookingid":6},{"bookingid":2},{"bookingid":8},{"bookingid":4},{"bookingid":5},{"bookingid":3},{"bookingid":10},{"bookingid":12},{"bookingid":7},{"bookingid":14},{"bookingid":9}]

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.

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

Your email address will not be published. Required fields are marked *