Make Selenium Easy

REST Assured Tutorial 7 – Builder Pattern in REST Assured

As a part of End to End REST Assured Tutorial , in this post We will learn about “Builder pattern in Java and how REST Assured uses it”.

If you refer any tutorial or official website of Rest Assured, they start with an examples as below:-

Beginners may confuse easily with syntax. THe purpose of my blog is to make you understand concept easily. So forget about above example and learn the basics first.

Suppose there is a Java class and it contains three non-static methods and I will call each method as below:-

Output:-

You should notice that I kept return type of all methods as “void”. I called each method individually using object name. Let’s change all return type from “void” to same as class name and return “this” as it always points to current object.

Output:-

It is also called method chaining. This way helps you to call methods as a chain.

REST Assured uses the same concept extensively and helps you to create chained script. For example, RequestSpecification interface. This interface is mainly used to add headers , params, authentication , body, cookies , proxy etc to request body. The return type of maximum methods in this interface is of type RequestSpecification. So that you can create chaining methods as below:-

Above code is same as below:-

So you no need to get confuse with above syntax. I have already discussed about Static import concept in Java. REST Assured uses both these concepts extensively. And good thing is that it is not mandatory. You can practise to follow the pattern to become comfortable.

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

You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.

Table of Contents

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

5 thoughts on “REST Assured Tutorial 7 – Builder Pattern in REST Assured

  1. Of all the tutorials I found yours the best, from where you learned all these concepts. I see everywhere people just try to solve the problem. but you hit at the core concepts which is helpful

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.