Example Program
package javaPackageName;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.specification.RequestSpecification;
public class JavaClassName {
@Test
public static void main(String[] args) {
/* Creation of RequestSpecBuilder object */
RequestSpecBuilder request_builder = new RequestSpecBuilder();
/* Base URI setup */
request_builder.setBaseUri("https://dummy.restapiexample.com/api/v1");
/* Base Path setup */
request_builder.setBasePath("/employee/1");
/* Using builder() method for getting RequestSpecification reference */
RequestSpecification request_specification = request_builder.build();
/* Directly calling http verbs on RequestSpecification */
/*Response res_1= request_specification.get();
System.out.println(res_1.asString());*/
System.out.println("/*********************************/");
/* In the overloaded given() method,
we can also pass the RequestSpecification reference variable. */
Response res_2 = RestAssured.given(request_specification).get();
System.out.println(res_2.asString());
System.out.println("/*********************************/");
/* Using the spec() method.
We can also pass RequestSpecification. */
Response res_3 = RestAssured.given().spec(request_specification).get();
System.out.println(res_3.asString());
}
}

You can also try this code with Online Java Compiler
Run Code
Output
/*********************************/
{"status":"success","data":{"id":1,"employee_name":"Tiger Nixon","employee_salary":320800,"employee_age":61,"profile_image":""},"message":"Successfully! Record has been fetched."}
/*********************************/
{"status":"success","data":{"id":1,"employee_name":"Tiger Nixon","employee_salary":320800,"employee_age":61,"profile_image":""},"message":"Successfully! Record has been fetched."}
Note
You can get the following exception after executing the program:
“java.lang.NullPointerException: Cannot get property ‘assertionClosure’ on null object”
In Rest Assured, there is a bug that is still open. To execute the program successfully, comment out lines 26 and 27 that read "Response res_1= reqSpec.get();".
Frequently Asked Questions
Are REST assured and REST API the same?
A Java library called REST Assured is used to test RESTful APIs. It is frequently used to test web applications that handle JSON and XML. Additionally, it supports all RESTful methods, including PUT, GET, PATCH, POST, and DELETE.
What is the difference between Postman and REST assured?
Code reusability: Since the Java client is REST-assured, it is possible. It can't be done in Postman. Creating a data-driven framework: For each collection, we can only offer one data file to the Postman automation runner. However, there is no restriction for this with REST-assured.
What is REST API in testing?
REST API, sometimes referred to as RESTful API, is an API that complies with REST's restrictions and allows communication with RESTful web services. It is an open-source automation method for testing RESTful APIs for web apps. It is frequently used to test web applications that employ JSON and XML.
Is REST assured open-source?
Yes, REST assured is an open-source Java Domain-specific language (DSL) that makes testing REST service simple.
What type of testing is done with REST assured?
Java library Rest assured is used to test Restful Web services. It can be used to test web services that are XML and JSON-based.
Conclusion
In this article, we have learned about the RequestSpecBuilder class in REST Assured. Also, we have seen an example where we are using the RequestSpecBuilder class.
That's it from the article. I hope you all like it.
Also, you can check our courses and test series for your interview preparations: Coding Ninja Test Series, Coding Questions, Interview Preparation Resources, Coding Contests, Product-based Company Course, Data Science & ML course, Master Data Analytics, and ML for beginners. You can also refer to other IT subjects like compiler design and software engineering.
Happy Learning!