Automate RESTful API using RestAssured - {Basics #2}
Karthikeyan Rajendran
Selenium 3&4 | Spring Boot | Playwright | Cypress | Postman | RestAssured | Supertest | Restful API | GraphQL | Contract Testing | Micro-service | Gatling | K6 | Locust | JMeter | Performance Engineering | SQL
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;
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.
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.
Test Architect || Selenium || Java || RestAssured || Jenkins || CI/CD || Maven || C#||Performance Engineering
2 年Great. This is really useful
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
Test Lead at Ameex Technologies
2 年Super karthick its very useful
Senior Associate at JPMorgan Chase & Co. | Full Stack QA
2 年Love this