REST Assured Tutorial 16 – Building RequestSpecification Using RequestSpecBuilder
As a part of the End to End REST Assured Tutorial, in this post, we will learn about class RequestSpecBuilder and how it can be used to create RequestSpecification in Rest Assured.
RequestSpecification – How The Request Will Look Like
Multiple Ways Of Calling HTTP Methods On A RequestSpecification
There is another way of creating RequestSpecification in Rest Assured and that is by using class RequestSpecBuilder.
RequestSpecBuilder is a class in Rest Assured, which contains methods to set cookies, headers, multipart details, body, authentication, form parameters, query parameters, path parameters, base path, base URI, proxy, etc. These all are required to construct a Requestspecification. After adding all required details, we need to use “build()” method of RequestSpecBuilder class to get a RequestSpecification reference.
RequestSpecification helps us to reuse specifications.
package RestAssuredConcepts; import io.restassured.RestAssured; import io.restassured.builder.RequestSpecBuilder; import io.restassured.response.Response; import io.restassured.specification.RequestSpecification; public class RequestSpecBuilderExample { public static void main(String[] args) { // Creating an object of RequestSpecBuilder RequestSpecBuilder reqBuilder = new RequestSpecBuilder(); // Setting Base URI reqBuilder.setBaseUri("https://restful-booker.herokuapp.com"); // Setting Base Path reqBuilder.setBasePath("/booking"); // Getting RequestSpecification reference using builder() method RequestSpecification reqSpec = reqBuilder.build(); // Usage in different styles // We can directly call http verbs on RequestSpecification Response res1= reqSpec.get(); System.out.println(res1.asString()); System.out.println("======================"); // We can also pass RequestSpecification reference variable in overloaded given() method Response res2 = RestAssured.given(reqSpec).get(); System.out.println(res2.asString()); System.out.println("======================"); // We can also pass RequestSpecification using spec() method Response res3 = RestAssured.given().spec(reqSpec).get(); System.out.println(res3.asString()); } }
[{"bookingid":11},{"bookingid":13},{"bookingid":15},{"bookingid":1},{"bookingid":6},{"bookingid":2},{"bookingid":8},{"bookingid":4},{"bookingid":5},{"bookingid":3},{"bookingid":10},{"bookingid":12},{"bookingid":7},{"bookingid":14},{"bookingid":9}] ====================== [{"bookingid":11},{"bookingid":13},{"bookingid":15},{"bookingid":1},{"bookingid":6},{"bookingid":2},{"bookingid":8},{"bookingid":4},{"bookingid":5},{"bookingid":3},{"bookingid":10},{"bookingid":12},{"bookingid":7},{"bookingid":14},{"bookingid":9}] ====================== [{"bookingid":11},{"bookingid":13},{"bookingid":15},{"bookingid":1},{"bookingid":6},{"bookingid":2},{"bookingid":8},{"bookingid":4},{"bookingid":5},{"bookingid":3},{"bookingid":10},{"bookingid":12},{"bookingid":7},{"bookingid":14},{"bookingid":9}]
Note:- You might get an exception “java.lang.NullPointerException: Cannot get property ‘assertionClosure’ on null object” on running above program. There is an existing bug in Rest Assured which is in an open state as of now. You can comment line no 23 and 24 “Response res1= reqSpec.get();” to run program successfully.
You can download/clone the above sample project from here.
You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.
If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading
#HappyLearning