Taking first steps in API testing with REST Assured (IntelliJ IDEA + Java + Maven + TestNG)

  1. Start a New Project choosing Java Language and Maven Build System:
New project customisation
JDK configuration
Project structure

Test structure in REST Assured

We will use BDD approach and the given-when-then syntax for writing tests:

given() 
.param("key", "val")
.header("name", "foo")
.when()
.httpMethod()
.then()
.statusCode(XXX)
.body("prop", equalTo("someVal"));

Explanation

given() — passes request headers, query or path params, a body, and cookies. All these arguments are optional.

Installing TestNG and REST Assured

We will use REST Assured library to test REST APIs and TestNG framework to make automated tests more readable. For these, add the following dependencies in pom.xml after properties section:

<dependencies>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.6.1</version>
</dependency>
</dependencies>

Test writing

Before start, a new Java class file should be created in src/test/java/ path. The first test will be placed there. Let’s start!

import org.testng.annotations.Test;

import static io.restassured.RestAssured.*;

GET /pet/findByStatus

Check that GET /pet/findByStatus returns 200 OK:

@Test
public void getPetsByStatus() {
given()
.header("accept", "application/json")
.when()
.get("https://petstore3.swagger.io/api/v3/pet/findByStatus?status=available")
.then()
.assertThat().statusCode(200);
}

POST /pet

To validate the response body state, you may add hamcrest package in pom.xml file:

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
</dependency>
import static org.hamcrest.Matchers.equalTo;
{
"id": 10,
"name": "Tail",
"category": {
"id": 1,
"name": "Dogs"
}
}
@Test
public void createPet() {
String json = "{ \"id\": \"10\", \"name\": \"Tail\", \"category\": {\"id\": \"1\", \"name\": \"Dogs\"} }";
given()
.body(json)
.header("Content-Type", "application/json")
.when()
.post("https://petstore3.swagger.io/api/v3/pet")
.then()
.assertThat().statusCode(200)
.assertThat().body("id", equalTo(10))
.assertThat().body("name", equalTo("Tail"))
.assertThat().body("category.id", equalTo(1))
.assertThat().body("category.name", equalTo("Dogs"));
}

DELETE /pet/{petId}

Check that DELETE /pet/{petId} returns 200 OK and “Pet deleted” body:

@Test
public void deletePet() {
given()
.pathParam("petId", "10")
.header("Accept", "*/*")
.when()
.delete("https://petstore3.swagger.io/api/v3/pet/{petId}")
.then()
.assertThat().statusCode(200)
.assertThat().body(equalTo("Pet deleted"));
}

Debugging hint

If you want to see the response of requests you can add log().body() or log().all() after then() and observe the method response in run output:

Example of full response in run window

Summary

In this article we configured the Java project with REST Assured and Maven. Also, we added 3 basic yet reasonable API tests.

Useful links

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store