REST Assured Tutorial 4 – Let’s Write First GET REST Assured Test

As a part of End to End REST Assured Tutorial , in this post We will write first GET request in REST assured.

I will strongly recommended you to learn basics of API testing from here.

We will automate a GET request named “GetBookingIds” from Restful Booker. We will verify status code and line of request.

I will not start with BDD pattern or builder pattern as of now. I will go step by step so that you do not confuse with the flow.

Let’s start with some basic points about Rest Assured:-

  1. RestAssured is a class which consists many static fields and methods.
    It supports POST, GET, PUT, DELETE, HEAD, PATCH and OPTIONS requests and to verify the response of these requests.
  2. RestAssured has a static overloaded method named get() which returns a reference of Response interface. In fact return type of all http methods in RestAssured class is of type Response. This response contains every details returned by hitting request i.e. response body, response headers, status code, status lines, cookies etc.
  3. To validate response like status code or value , we need to get reference of type ValidatableResponse. ValidatableResponse is an interface. Response interface has a method named “then()” which returns ValidatableResponse. In fact there is an interface called “Validatable” which has “then()” method. Response interface extends Validatable Interface. The implemented class of Response interface is

    RestAssuredResponseImpl. We will see hierarchy of classes and interfaces later.

  4. Once we get ValidatableResponse reference, we can do many assertions. In this post, we will verify status code and status line. It consists of many validation methods.

Non-BDD Style Code:-

package RestfulBooker.getExamples;

import org.testng.annotations.Test;

import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;
import io.restassured.specification.RequestSpecification;

public class NonBDDStyleGetRequest {

        // Without static import and builder pattern
        @Test
        public void GetBookingIds_VerifyStatusCode() {
                
                
                // Create a request specification 
                RequestSpecification request= RestAssured.given();
                
                //Adding URI
                request.baseUri("https://restful-booker.herokuapp.com/booking");
                
                // Calling GET method on URI. After hitting we get Response
                Response response = request.get();
                
                // Let's print response body.
                String resString = response.asString();
                System.out.println("Respnse Details : " + resString);

                /*
                 * To perform validation on response like status code or value, we need to get
                 * ValidatableResponse type of response using then() method of Response
                 * interface. ValidatableResponse is also an interface.
                 */
                ValidatableResponse valRes = response.then();
                // It will check if status code is 200
                valRes.statusCode(200);
                // It will check if status line is as expected
                valRes.statusLine("HTTP/1.1 200 OK");
                
                

        }

}

BDD Style Code:-

package RestfulBooker.getExamples;

import org.hamcrest.Matchers;
import org.testng.annotations.Test;

import io.restassured.RestAssured;

public class BDDStyleGetRequest {

        
        @Test
        public void GetBookingIds_VerifyStatusCode() {
                
                // Given
                RestAssured.given()
                        .baseUri("https://restful-booker.herokuapp.com")
                // When
                .when()
                        .get("/booking")
                // Then
                .then()
                        .statusCode(200)
                        .statusLine("HTTP/1.1 200 OK")
                        // To verify booking count
                        .body("bookingid.sum()", Matchers.hasSize(10))
                        // To verify booking id at index 3
                        .body("bookingid[3]", Matchers.equalTo(1));                   
                

        }

}

BDD with static import style code:-

package RestfulBooker.getExamples;

import static org.hamcrest.Matchers.*;
import static io.restassured.RestAssured.*;
import org.testng.annotations.Test;

public class BDDStyleGetRequestWithStaticImport {

        
        @Test
        public void GetBookingIds_VerifyStatusCode() {
                
                // Given
                given()
                        .baseUri("https://restful-booker.herokuapp.com")
                // When
                .when()
                        .get("/booking")
                // Then
                .then()
                        .statusCode(200)
                        .statusLine("HTTP/1.1 200 OK")
                        // To verify booking count
                        .body("bookingid", hasSize(10))
                        // To verify booking id at index 3
                        .body("bookingid[3]", equalTo(1));                    
                

        }

}

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