Automate RESTful API using RestAssured - {Basics #2}

Hallo Amigos,

Welcome back! First of all, thank you for your support, comments, and encouragement, which boosted me and provided more information to our learning community. If you have not read my first article, please find this link and read the basics before starting this article.

In this article, we will begin our learning journey with the REST-assured library.

What is REST-assured?

REST-assured is an open-source Java library that makes it easy to write automated API tests. This library not only facilitates API requests but also verifies responses.

The REST-assured library performs two main actions;

  1. Sending requests and getting the relevant response
  2. Validate the response with multiple assertions

Based on the above-mentioned two actions, I am going to divide this article into two parts. The first one is hitting the GET endpoint and printing the response in the console, and the second one is validating the same response with multiple assertion conditions using the Hamcrest assertion library.

The REST-assured library is structured based on the Gherkin language (GIVEN, WHEN, THEN) format and was designed using the Bridge and Chain of Responsibility design patterns.

GIVEN: Specify prerequisites (include headers, authentication, or even setting request parameters)
WHEN: Describe the action to take (HTTP Method)
THEN: Describe the expected result

Create a Maven project in any IDE that is convenient for you and add the following mentioned Maven dependencies to the pom.xml file and save it.

<dependencies>
		<dependency>
			<groupId>io.rest-assured</groupId>
			<artifactId>rest-assured</artifactId>
			<version>5.3.0</version>
		</dependency>
		<dependency>
			<groupId>org.testng</groupId>
			<artifactId>testng</artifactId>
			<version>6.14.3</version>
		</dependency>
</dependencies>        

After adding the relevant dependencies in the pom.xml file, create a TestNG test class under the "com.linkedin.rest.assured.article.two" package.

First, add a static import of the RestAssured class library in the test class

import static io.restassured.RestAssured.*;        

What is static import?

In Java, the concept of static import was introduced in version 1.5. With static import, we can directly access the static members of a class without having to use the class name or create an object.

GET Call using REST-assured Library

It's time to get our hands dirty with coding. In this section, we will use the REST-assured Java library to call the GET method of a demo RESTful API.

Initially, we'll examine how to send requests and display the corresponding response in the console.
package com.linkedin.rest.assured.article.two

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

public class GetCallPrintResponse {	

	@Test
	public void printWelcomeMessage() {
		given()
		.when()
		? ? .get("https://simple-books-api.glitch.me")
		.then()
		? ? .log()
		? ? .body();
	}

}        
Output
{
? ? "message": "Welcome to the Simple Books API."
}        

Let's examine the code above.

  1. The given() method specifies the prerequisites for the GET call, such as headers, authorization, or parameters, but for hitting the given endpoint we don't need any prerequisites.
  2. The when() method describes the action to be taken, which in this case is a GET method call to a specified endpoint URL.
  3. The then() method outlines the expected result. In this case, we used the log().body() method to print the response in the console.

Now we're going to see the second part of the REST-assured library, which involves validating the correctness of the response.
package com.linkedin.rest.assured.article.two

import org.testng.annotations.Test;

import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

public class GetCallPrintResponse {	

	@Test
	public void validateWelcomeMessage() {
		given()
		.when()
		? ? .get("https://simple-books-api.glitch.me")
		.then()
		? ? .assertThat()
		? ? .statusCode(200)
		? ? .body("message", equalTo("Welcome to the Simple Books API."));
	}

}        

Let's examine the code above.

Under the then() method, we add two assertions to validate the response. The first one checks if the response status code is 200, and the second one validates the JSON response body. The REST-assured library uses the Hamcrest assert library for asserting the JSON response body. To use the Hamcrest equalTo() method, we need to add a static import of the Hamcrest Matcher class.

import static org.hamcrest.Matchers.*;        

In the reference section, I will add a link to my GitHub repository, where I will upload the code for all my articles. If you have any doubts, feel free to reach out to me and I will do my best to assist you. Thank you for taking the time to read this article. In my next article, I will cover JSON Path, query parameters, and basic authentication. Happy learning! Until next time.

Reference

Bargavi Samulunaidu Mani

Test Architect || Selenium || Java || RestAssured || Jenkins || CI/CD || Maven || C#||Performance Engineering

2 年

Great. This is really useful

Gajalakshmi Venugopal

Technical QA Manager | Driving Cost Savings & Enhancing Quality Assurance with Strategic Test Approaches | Skilled in Automation & Programming | Expertise in Advanced Tools for Peak Project Efficiency

2 年

Just what was needed

回复
Manikaprabu K

Test Lead at Ameex Technologies

2 年

Super karthick its very useful

J J.

Senior Associate at JPMorgan Chase & Co. | Full Stack QA

2 年

Love this

要查看或添加评论,请登录

Karthikeyan Rajendran的更多文章

  • Why interfaces are used in test automation frameworks?

    Why interfaces are used in test automation frameworks?

    Back Story: One day at the cafeteria, a colleague and I casually discussed the framework we designed. He raised a…

    2 条评论
  • Automate RESTful API using RestAssured - {Basics #1}

    Automate RESTful API using RestAssured - {Basics #1}

    Hallo Amigos, This is my first step in the new year 2023. I would like to share my knowledge in the QA/testing world.

    11 条评论
  • Basics of Security Testing

    Basics of Security Testing

    Hi Friends, Today, I took a wonderful session on the topic of "Basics of Security Testing". I have shared a great chunk…

    3 条评论
  • How do you describe Scrum?

    How do you describe Scrum?

    #Agile #Scrum While last week is was getting one post from linked In group. Which was, “What’s the best way to describe…

    8 条评论
  • Do's and Don'ts For a Tester

    Do's and Don'ts For a Tester

    Hi Friends, Today, I took a wonderful session on the topic of "Do's and Don'ts For a Tester". From my experience and…

    2 条评论
  • Tester Must Need Four Basic Good Communications With The Development Teammates

    Tester Must Need Four Basic Good Communications With The Development Teammates

    Tester must need four basic good communications with the development teammates. Good communicators know how to explain…

  • How many (what percentage of) test cases do you automate?

    How many (what percentage of) test cases do you automate?

    “How many (what percentage of) test cases do you automate?”. It is a quite common question raised from the managers.

  • Selenium IDE Firefox add-on end of life!!!

    Selenium IDE Firefox add-on end of life!!!

    The Selenium IDE add-on for Firefox, an add-on used to automate tests in Firefox for web development and other testing…

    2 条评论
  • The race between Automation Testing(Rabbit) and Manual Testing(Tortoise)

    The race between Automation Testing(Rabbit) and Manual Testing(Tortoise)

    Shall we start the race between Manual Testing and Automation Testing……………… Oh! Yaaaaaaaaaaa…………………. One,,,,,,,,,,,,…

  • A/B Testing Certification

    A/B Testing Certification

    In current trends, data involves a product, service, or content in the each and every page of a website holds valuable…

社区洞察

其他会员也浏览了