Reducing Code duplication with RequestSpecBuilder approach


Reducing Code duplication with RequestSpecBuilder approach

Hi, all welcome to another of my lockdown blog series posts.  When writing the RestAssure test it is important that we reduce the number of code duplication. This will help in maintenance as well as readability. In this blog post, I will show with a simple example of how we can use the RequestSpecBuilder to achieve this.

Let's take an example where we want to pass multiple country and postal codes to get information related to those values passed. If we want to write multiple tests one way is to have the baseURL and other parameters such as ContentType duplicated. To overcome this we can make use of RequestSpecBuilder. If we look at the below code we have a method that returns a RequestSpecification. Within that method, we have the base URL and content type specified. Hear, all that the test needs to do is to call the return value within the given().spec() within all the tests that we implement. 

With this approach, we can avoid duplicating the same code multiple times and help to achieve maintainable code.

package SpecBuilder; 

import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification; 
import io.restassured.builder.RequestSpecBuilder; 
import org.testng.annotations.BeforeTest; 
import org.testng.annotations.Test; 
import static io.restassured.RestAssured.given;
 

public class SpecBuilderTest {

    //private static RequestSpecification requestSpecification;
    @BeforeTest 

     public RequestSpecification requestSpec(){
        RequestSpecification requestSpecification =new RequestSpecBuilder()
                .setBaseUri("http://api.zippopotam.us")
                .setContentType(ContentType.JSON)
                .build(); 

        return requestSpecification; 

    }

    @Test 

    public void testLKCode() {

        given().
                spec(requestSpec()).
                when().log().all().
                get("/lk/10350").
                then().assertThat()
                .statusCode(200); 

     }

    @Test 

    public void testUSCode(){
        given().
                spec(requestSpec()).
                when().log().all().
                get("/us/90201").
                then().assertThat()
                .statusCode(200); 

       }
}

In the same way, we can use the ResponseSpecBuilder to help reduce the code duplication for assertions. As shown below the same test we wrote above is changed so that each test does not have the assertion but has a method of type ResponseSpecification which can be called into the test.


package SpecBuilder; 

import io.restassured.builder.ResponseSpecBuilder; 
import io.restassured.http.ContentType; 
import io.restassured.specification.RequestSpecification;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.specification.ResponseSpecification; 
import org.testng.annotations.BeforeTest; 
import org.testng.annotations.Test; 
import static io.restassured.RestAssured.given;
 
public class SpecBuilderTest {

    //private static RequestSpecification requestSpecification;
    @BeforeTest

     public RequestSpecification requestSpec(){
        RequestSpecification requestSpecification =new RequestSpecBuilder()
                .setBaseUri("http://api.zippopotam.us")
                .setContentType(ContentType.JSON)
                .build(); 

        return requestSpecification; 

    }

    public ResponseSpecification responseSpec(){
        ResponseSpecification responseSpecification=new ResponseSpecBuilder().
                expectStatusCode(200).
                expectContentType(ContentType.JSON).
                build(); 

        return  responseSpecification; 

    }
    @Test 

    public void testLKCode() {

        given().
                spec(requestSpec()).
                when().log().all().
                get("/lk/10350").
                then()
                .spec(responseSpec()); 

   }

    @Test 

    public void testUSCode(){
        given().
                spec(requestSpec()).
                when().log().all().
                get("/us/90201").
                then()
                .spec(responseSpec()); 

    }
}

Comments