Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 02/19/2025 By admin

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

Uncategorized

Post navigation

Previous Post: Java Programs 13: Java Program to Print Fibonacci Series of Given Length Using Collection
Next Post: Part 4: Waits In Selenium: Fluent Wait

Related Posts

noSuchElementException Uncategorized
REST Assured Tutorial 29 – How to create POJO classes of a JSON Object Payload Uncategorized
TestNG Tutorials – Page 2 Uncategorized
Untitled Diagram (1) – Make Selenium Easy Uncategorized
SelectorsHub : An XPath & cssSelector dictionary!! Uncategorized
TestNG Tutorials 54: DataProvider in TestNG – Implementing Object Oriented Concept “Encapsulation” with DataProvider Method Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark