Table of contents
1.
Introduction
2.
Setting A Default RequestSpecification In Rest Assured
2.1.
Rest Assured Program
3.
Querying RequestSpecification In Rest Assured
3.1.
Rest Assured Program
4.
Frequently Asked Questions
4.1.
Can we make multiple Request Specification?
4.2.
How can we easily set default RequestSpecification in Rest Assured?
4.3.
How can we retrieve a query from RequestSpecification?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

REST Assured – Setting Default and Querying RequestSpecification

Author Tisha Chhabra
0 upvote

Introduction

We have learned about different concepts for Rest assured in our different articles. In this article, we will learn about setting a default RequestSpecification. Also, we will see all about the Querying RequestSpecification in Rest Assured.

REST Assured

Setting A Default RequestSpecification In Rest Assured

In Rest Assured, we can make multiple Request Specification as per our requirements. Rest Assured provides a way to set the default Request Specification so that we can send each request if no other Request Specification is set. In Java, the default value of an int data type is zero. Similarly, we can fix a Request Specification as default in Rest Assured.

For setting a default RequestSpecification in Rest Assured, we have to use the static property “requestSpecification“of RestAssured class. When no other RequestSpecification is set to request, default RequestSpecification is sent. If we are setting another request specification to the request, the default request specification will be overridden by the new one.

Rest Assured Program

package javaPackageName;
​
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 RARequestSpecificationSettingDefault {
​
@BeforeClass
public void SetupDefault()
{
/*Using given() to create Request Specification*/
RequestSpecification objreq1= RestAssured.given();
/*Base URI*/
objreq1.baseUri("https://reqres.in/api/users");
/*Base Path*/
objreq1.basePath("/2");

RestAssured.requestSpecification = objreq1;
}


@Test
public void UseDefault()
{
/*Use default RequestSpecification*/
Response res = RestAssured.when().get();
System.out.println("Response for default: "+res.asString());
}

@Test
public void OverrideDefault()
{
/*Using with() to create request specification*/
RequestSpecification objreq2= RestAssured.with();
/*Base URI*/
objreq2.baseUri("https://reqres.in/api");
/*Base Path*/
objreq2.basePath("/users?page=3");
/*Override default Request Specification*/
Response res = RestAssured.given().spec(objreq2).get();
System.out.println("Response for overriding: "+res.asString());
}
}


Output:

output

Querying RequestSpecification In Rest Assured

As we all know that RequestSpecification is used to make a request or how the sent request will look like. We set a base path, headers, base URI, etc to request using RequestSpecification. There is a different way available also to query this RequestSpecification so that at any point in the time, we can get details of RequestSpecification like what base URI is set, etc.

To retrieve or query details from RequestSpecification, Rest Assured provides a class named SpecificationQuerier. This class contains a static method which is called query(RequestSpecification specification) which returns the reference of the QueryableRequestSpecification interface. This interface has different methods to retrieve request details such as getBody(), getBasePath(), etc.

Rest Assured Program

package javaPackageName;
​
import org.testng.annotations.Test;
​
import io.restassured.RestAssured;
import io.restassured.specification.QueryableRequestSpecification;
import io.restassured.specification.RequestSpecification;
import io.restassured.specification.SpecificationQuerier;
​
public class RARequestSpecificationQuerying {
@Test
public void function() {

/*Using given() to create Request Specification*/
RequestSpecification objreq1= RestAssured.given();
/*Base URI*/
objreq1.baseUri("https://reqres.in/api/users")
/*Base PATH*/
.basePath("/2");
/*Querying -> RequestSpecification using query()*/
QueryableRequestSpecification objQueryreq = SpecificationQuerier.query(objreq1);

/*Get Base URI*/
String objgetURI = objQueryreq.getBaseUri();
System.out.println("Base URI - "+objgetURI);

/*Get Base PATH*/
String objgetPath = objQueryreq.getBasePath();
System.out.println("Base PATH - "+objgetPath);
}
}


Output:

output

Frequently Asked Questions

Can we make multiple Request Specification?

Yes, we can make multiple Request Specification as per our requirements. 

How can we easily set default RequestSpecification in Rest Assured?

For setting a default RequestSpecification in Rest Assured, we have to use the static property “requestSpecification“of RestAssured class.

How can we retrieve a query from RequestSpecification?

To retrieve or query details from RequestSpecification, Rest Assured provides a class named SpecificationQuerier. This class contains a static method which is called query(RequestSpecification specification) which returns the reference of the QueryableRequestSpecification interface. 

Conclusion

In this article, we have learned about setting a Default RequestSpecification and Querying RequestSpecification in Rest Assured. You can learn the basics of Java and data structures and algorithms in Java on Coding Ninjas.

Refer to our guided path on code studio to learn more about DSA Competitive Programming, Javascript System Design, etc. Enroll in our courses and refer to the mock test and problems available. Also, look at the interview experiences for placement preparations.

Happy Learning Ninjas.

Live masterclass