Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 02/19/2025 By admin

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

Uncategorized

Post navigation

Previous Post: Git Tutorial 30 – Understand Soft, Mixed And Hard Options Of Git Reset
Next Post: JavaByTopics

Related Posts

Bedsheets – Make Selenium Easy Uncategorized
Learn Selenium With Quiz – Basic Level 6 – XPath Uncategorized
Postman Tutorial Part 15 – Adding Automation Test Scripts In Postman Uncategorized
image – Make Selenium Easy Uncategorized
Features of Selenium 4.0.0 Release – Java Binding Uncategorized
selenium testng tutorials Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark