Table of contents
1.
Introduction
2.
Class RequestSpecBuilder
3.
Example Program
3.1.
Note
4.
Frequently Asked Questions
4.1.
Are REST assured and REST API the same?
4.2.
What is the difference between Postman and REST assured?
4.3.
What is REST API in testing?
4.4.
Is REST assured open-source?
4.5.
What type of testing is done with REST assured?
5.
Conclusion
Last Updated: Mar 27, 2024

REST Assured – Building RequestSpecification Using RequestSpecBuilder

Author soham Medewar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

rest assured

REST Assured is a library in Java. It provides a domain-specific language for writing. Also, provide maintainable tests for RESTful APIs. Rest Assured is utilized to test the REST APIs. Java libraries act on the Rest web services like a headless client. The Rest Assured-based libraries are also capable of validating the server's HTTP answers.

In this blog, we will be learning about the RequestSpecBuilder class. Also, we will be using it to create RequestSpecification in REST Assured. 

Class RequestSpecBuilder

RequestSpecBuilder

The RequestSpecBuilder class in REST Assured contains several methods. These methods help to set cookies, multipart details, authentication, body, form parameters, headers, path parameters, base URI, base path, query parameters, proxy, etc. All these methods are required to build a RequestSpecification. Finally, after adding all the essential details, we use the build() method of RequestSpecBuilder class and get a RequestSpecification reference. RequestSpecification helps us to reuse the specifications. 

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 SeriesCoding QuestionsInterview Preparation ResourcesCoding ContestsProduct-based Company Course, Data Science & ML courseMaster Data Analytics, and ML for beginners. You can also refer to other IT subjects like compiler design and software engineering

Happy Learning!

Live masterclass