REST Assured Tutorial 17 – Setting a Default RequestSpecification in Rest Assured

As a part of End to End REST Assured Tutorial , in this post We will learn about “How to set a default RequestSpecification which will be set to each request in Rest Assured?“.

This is an interview question as well. Let’s understand the meaning of this first. We know well now that we can create multiple Request Specification as per our need. Rest Assured provides a way to set a default Request Specification so that it can be sent to each request if no other Request Specification is set. In Java, default value of an int data type is zero. Similar to that we can set a Request Specification as default in Rest Assured.

To set a default Request Specification, we need to use static property “requestSpecification“of RestAssured class. When no other RequestSpecification is set to request, default RequestSpecification will be sent. If we set another request specification to the request, default request specification will be overridden by new one.

Rest Assured Code:-

package RestAssuredConcepts;

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

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

public class DefaultRequestSpecificationExample {

        @BeforeClass
        public void setupDefaultRequestSpecification()
        {
                // Creating request specification using given()
                RequestSpecification request1= RestAssured.given();
                // Setting Base URI
                request1.baseUri("https://restful-booker.herokuapp.com");
                // Setting Base Path
                request1.basePath("/booking");
                
                RestAssured.requestSpecification = request1;
        }
        
        
        @Test
        public void useDefaultRequestSpecification()
        {
                // It will use default RequestSpecification
                Response res = RestAssured.when().get();
                System.out.println("Response for default: "+res.asString());
        }
        
        @Test
        public void overrideDefaultRequestSpecification()
        {
                // Creating request specification using with()
                RequestSpecification request2= RestAssured.with();
                // Setting Base URI
                request2.baseUri("https://restful-booker.herokuapp.com");
                // Setting Base Path
                request2.basePath("/ping");
                // Overriding default request specification
                Response res = RestAssured.given().spec(request2).get();
                System.out.println("Response for overriding: "+res.asString());
        }
}

Output:-

Response for overriding: Created
Response for default: [{"bookingid":7},{"bookingid":5},{"bookingid":9},{"bookingid":3},{"bookingid":8},{"bookingid":1},{"bookingid":4},{"bookingid":6},{"bookingid":10},{"bookingid":2}]

You can clone/download example repo here.

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning