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.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply