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:-

@Test public void
lotto_resource_returns_200_with_expected_id_and_winners() {

    when().
            get("/lotto/{id}", 5).
    then().
            statusCode(200).
            body("lotto.lottoId", equalTo(5),
                 "lotto.winners.winnerId", hasItems(23, 54));

}

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:-

package JavaConcepts;

public class NonBuilderPattern {
	
	public void M1()
	{
		System.out.println("M1");
	}
	
	public void M2(String str)
	{
		System.out.println("Pass string is "+str);
	}
	
	public void M3()
	{
		System.out.println("M3");
	}

	public static void main(String[] args) {
		
		NonBuilderPattern nbp = new NonBuilderPattern();
		nbp.M1();
		nbp.M2("Amod");
		nbp.M3();
	}
}

Output:-

M1
Pass string is Amod
M3

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.

package JavaConcepts;

public class BuilderPattern {
	
	// Change return type of each method as Class type
	// "this" always points to current/calling object. Returning the same to
	// have same reference
	public BuilderPattern M1()
	{
		System.out.println("M1");
		return this;
	}
	
	public BuilderPattern M2(String str)
	{
		System.out.println("Pass string is "+str);
		return this;
	}
	
	public BuilderPattern M3()
	{
		System.out.println("M3");
		return this;
	}

	public static void main(String[] args) {
		
		BuilderPattern nbp = new BuilderPattern();
		nbp.M1().M2("Amod").M3();
	}
}

Output:-

M1
Pass string is Amod
M3

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:-

RequestSpecification req= RestAssured.given()
		.accept(ContentType.JSON)
		.auth().preemptive().basic("username", "password")
		.header("headername", "headervalue")
		.param("paramname", "paramvalue")
		.cookie("cookieName", "value");
 

Above code is same as below:-

RequestSpecification req= RestAssured.given();
		req= req.accept(ContentType.JSON);
		req= req.auth().preemptive().basic("username", "password");
		req= req.header("headername", "headervalue");
		req= req.param("paramname", "paramvalue");
		req= req.cookie("cookieName", "value");

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.

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

Your email address will not be published. Required fields are marked *