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:

.param("key", "val")
.header("name", "foo")
.body("prop", equalTo("someVal"));


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:


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:

public void getPetsByStatus() {
.header("accept", "application/json")

POST /pet

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

import static org.hamcrest.Matchers.equalTo;
"id": 10,
"name": "Tail",
"category": {
"id": 1,
"name": "Dogs"
public void createPet() {
String json = "{ \"id\": \"10\", \"name\": \"Tail\", \"category\": {\"id\": \"1\", \"name\": \"Dogs\"} }";
.header("Content-Type", "application/json")
.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:

public void deletePet() {
.pathParam("petId", "10")
.header("Accept", "*/*")
.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


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