Using RequestSpecBuilder in Rest Assured ( Code Reuse )



In the previous post, we studied about how to check response time using Rest Assured. In this post, we would learn about using RequestSpecBuilder in Rest Assured. You can use the builder to construct a request specification. RequestSpecBuilder is normally used when we have common parameters, Base URI, Base Path between different tests.

Let's look at an example without RequestSpecBuilder. Here we are making use of Google Search API that we studied in Testing GET Requests and their Responses using Rest Assured.

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import static io.restassured.RestAssured.*;


public class GetStatusCodeTest {

 @BeforeClass
  public void setBaseUri () {

    RestAssured.baseURI = "https://maps.googleapis.com";
  }


@Test
  public void test01 () {
    
    given().param ("query", "restaurants in mumbai")
        .param ("key", "xyz")
        .get ("/maps/api/place/textsearch/json")
        .then ()
        .contentType(ContentType.JSON)
        .assertThat ().statusCode (200);

  }

}

In above example, the parameters to be sent i.e query and key are mentioned in test02 as well as the base path "/maps/api/place/textsearch/json" is mentioned in test02Now we use ‘then‘ part to check the response. We simply add a check ‘contentType(ContentType.JSON)‘ to make sure that the response we get is in JSON format.Under assertThat method, we add a check that the statusCode (200) is returned in response.  

It may happen that we have multiple tests which may take same parameters ( query and key ) and base path. In this case, RequestSpecBuilder will come handy.

Now let us have a look at the same example with RequestSpecBuilder.


import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import io.restassured.builder.RequestSpecBuilder;
import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification;

import static io.restassured.RestAssured.*;

public class RequestSpecificationTest {

  RequestSpecification rspec;
  RequestSpecBuilder build;
  
  @BeforeClass
  public void requestSpec () {
    
    build = new RequestSpecBuilder();
    build.setBaseUri ("https://maps.googleapis.com");
    build.setBasePath ("maps/api/place/textsearch/json");
    build.addParam ("query", "restaurants in mumbai");
    build.addParam ("key", "XYZ");
    
    rspec = build.build ();

 }
  
  @Test
  public void test01 () {
    
         given()
        .spec (rspec)
        .when ()
        .get ("")
        .then ()
        .contentType (ContentType.JSON)
        .statusCode (200);     
  }
  
}

For requestSpec method, we specify a BeforeClass annotation of TestNG so that it would run before the tests mentioned in the class. In requestSpec method, we create an object of RequestSpecBuilder.
Once the object is created then we set BaseUri, BasePath and add Parameters.

In test01 method, we just use RequestSpecification rspec, instead of adding parameters ( query and key ) and base path in the test. This functionality is useful when you have to send the request with same set of data.

In the next post, we would learn about ResponseSpecBuilder in Rest Assured.

Comments

  1. Slots Casino Review - Jtm Hub
    At 의왕 출장마사지 Jtm, you can play more than 700 slots and over 300 table games. We're going to focus 경상북도 출장안마 on 보령 출장샵 creating a new 안동 출장안마 casino with the 영주 출장마사지 most

    ReplyDelete

Post a Comment

Popular posts from this blog

Extracting an XML Response with Rest-Assured