Table of contents
1.
Introduction
2.
REST Assured
3.
Builder Pattern
3.1.
Implementation without Builder Pattern
3.1.1.
Example.1
3.1.2.
Output
3.2.
Implementation with Builder Pattern
3.2.1.
Example.2
3.2.2.
Output
3.3.
Without the implementation of the Builder pattern in REST Assured
3.3.1.
Output
3.4.
Implementation of the Builder Pattern in REST Assured
3.4.1.
Output
4.
Frequently Asked Questions
4.1.
What is RequestSpecBuilder?
4.2.
What is REST API testing?
4.3.
Why do we use REST assured?
4.4.
What are the request specs?
4.5.
What is the use of a request builder?
5.
Conclusion 
Last Updated: Mar 27, 2024
Medium

Builder Pattern in REST Assured

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hello Ninjas! Today we will discuss Rest assured in Java and the builder pattern in REST Assured.

Builder Pattern in REST Assured

REST (Representational State Transfer) is an architectural paradigm for establishing web services that define requirements. REST API is a straightforward and flexible approach to accessing online services without going through any processing.

Regarding Rest assured, let's move towards REST assured, and then we will discuss the builder pattern in REST assured.

REST Assured

Rest assured is a Java library that integrates well with Maven and enables us to test RESTful APIs. It is generally famous for testing JSON and XML-based web applications

REST Assured

Nearly every section of the request and response can be retrieved data using Rest Assured's techniques. REST Assured supports all REST methods, such as GET, PUT, POST, PATCH, and DELETE.

Builder Pattern

Builder Pattern is one of the design patterns used in test automation. It reduces the number of parameters required for a constructor. It allows the creation of multiple representations of an object. The builder pattern’s main aim is to separate a complex object's construction from its representation. 

Builder Pattern

 

Let’s say we have a user object that has four attributes. Among these, two fields are mandatory, the rest are optional, and the requirement is to create an immutable class. So how will we manage? by creating more constructors? Of course, a big “NO”! Otherwise, we will lose readability and immutability. 

We can solve the same problem by using the builder pattern. In the builder design pattern, we don't have to pass in null for the optional parameters to the constructor. Since the builder pattern helps minimize the number of parameters in the constructor. 

Let’s now understand the concept of a builder pattern with the help of two examples, one without a builder pattern and the other by implementing the builder pattern. 

Implementation without Builder Pattern

Now, let’s create a student class, and here we will call each method without implementing the builder pattern. 

Example.1

In the below snapshot, you can see the implementation of the getter and setter methods of these three attributes- 

  • id 
  • FirstName
  • LastName
Without implementing Builder Pattern

Now, we will set the values of all three attributes using the setter method that will be called via the Student class object.

Without implementing Builder Pattern img

Output

Output image

Implementation with Builder Pattern

Now, we will again create a student class, and this time we will call each method by implementing the builder pattern. 

Example.2

Now, in the given code snippet, we can see the implementation of the builder pattern.

To implement the builder pattern, we will replace all void keywords used in setMethods with the class name, which is ‘Student’ in this example. We will also use this keyword and return the object of a given class.

Implementing Builder Pattern

Now in the below snapshot, we can see the implementation of the builder pattern where the ‘Student’ class is created and yet not repeated,

Implementing Builder Pattern image

Output

output img

I hope till here you are clear with the basic implementation of the builder pattern in the normal java class. Let’s now look into the implementation of the builder pattern in REST assured.

Without the implementation of the Builder pattern in REST Assured

REST Assured uses the same concept and helps you to create the chained script. In the below code, we have created the RequestSpecification object named req. This object is mainly used to call paramscontent typeauthenticationbody, status code, etc., to request body. 

Without the implementation of the Builder pattern in REST Assured

Here we have taken an example of the Patch method, which is used to modify the data. As you can see here, we have passed the modified values in the body param

Output

output of Without the implementation of the Builder pattern in REST Assured

Implementation of the Builder Pattern in REST Assured

The return type of maximum methods is of type RequestSpecification. So that the chaining methods can be created as below-

Implementation of the Builder Pattern in REST Assured

Output

output

This was all about the implementation of the builder pattern in REST assured. Now it’s time for some frequently asked questions.

Frequently Asked Questions

What is RequestSpecBuilder?

RequestSpecBuilder is a class in Rest Assured. It contains methods that help set cookies, multipart details, header, body, authentication, query parameters, form parameters, path parameters, base URI, base path, proxy, etc.

What is REST API testing?

REST API testing is an open-source technique for testing RESTful APIs for web applications. It is commonly used to test JSON and XML-based web applications. It works with all methods, including GET, PUT, POST, PATCH, and DELETE. The REST API is a Java library.

Why do we use REST assured?

It eliminates the need to write a large amount of source code to establish an HTTP connection, send, and receive a request and parse a response.

What are the request specs?

Request specs help you to focus on a single controller action. But unlike controller tests, it includes the router and the middleware stack. It also consists of both rack requests and responses.

What is the use of a request builder?

We can use a request builder to configure the per-request state. We can also configure the request URI, the request method, specific request headers, etc. The default request method is GET unless it is explicitly set. Each setter method modifies or changes the builder's state and returns the same instance.

Conclusion 

In this article, we have extensively discussed the builder pattern in REST assured. We hope that this article has provided you with the help to enhance your knowledge regarding the builder pattern in REST assured, and if you would like to learn more, Do check out these articles.

 

Refer to our guided paths on Coding Ninjas 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. Take a look at the interview experiences and interview bundle for placement preparations.

Keep Coding!

Live masterclass