REST Assured Tutorial: Building a REST Assured API Automation Test Program from 0 to 1

Nao
6 min readNov 2, 2023

--

REST Assured supports both Gradle and Maven build tools, you can choose one of them according to your preference. Below is a description of the initialization process for Gradle and Maven build tools.

This project is built using Gradle 8.44 and Maven 3.9.5, if you are using other versions, it may be different.

Gradle version

See the demo project at https://github.com/Automation-Test-Starter/RestAssured-gradle-demo.

Initialize an empty Gradle project

mkdir RestAssured-gradle-demo
cd RestAssured-gradle-demo
gradle init

Configuration build.gradle

The demo project introduces the testNG testing framework. For reference only.

  • Create a build.gradle file in the project root directory to configure the project.
  • For reference, the following is a sample configuration
// plugins configuration
plugins {
id 'java' // use java plugin
}
// repositories configuration
repositories {
mavenCentral() // user maven central repository
}
// dependencies configuration
dependencies {
testImplementation 'io.rest-assured:rest-assured:5.3.1' // add rest-assured dependency
testImplementation 'org.testng:testng:7.8.0' // add testng testing framework dependency
implementation 'org.uncommons:reportng:1.1.4' // add testng reportng dependency
implementation 'org.slf4j:slf4j-api:2.0.9' // add slf4j dependency for test logging
implementation 'org.slf4j:slf4j-simple:2.0.9' // add slf4j dependency for test logging
implementation group: 'com.google.inject', name: 'guice', version: '7.0.0'
}
// test configuration
test {
reports.html.required = false // set gradle html report to false
reports.junitXml.required = false // set gradle junitXml report to false
// use testng testing framework
useTestNG() {
useDefaultListeners = true
suites 'src/test/resources/testng.xml' // set testng.xml file path
}
testLogging.showStandardStreams = true // output test log to console
testLogging.events "passed", "skipped", "failed" // deny output test log to console
}

You can copy the contents of the build.gradle file in this project. For more configuration refer to Official Documentation

testng.xml configuration

  • Create a resources directory under the src/test directory to store test configuration files.
  • Create a testng.xml file in the resources directory to configure the TestNG test framework.
  • For reference, the following is a sample configuration
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="restAssured-gradleTestSuite">
<test thread-count="1" name="Demo">
<classes>
<class name="com.example.TestDemo"/> <!-- test case class-->
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

gradle build project and initialize

  • Open the Terminal window of the project with an editor and execute the following command to confirm that the project build was successful
gradle build
  • Initialization complete: After completing the wizard, Gradle will generate a basic Gradle project structure in the project directory

initialization project directory

The directory structure can be found in » Project structure

Create a new test class in the project’s test source directory. By default, Gradle usually places the test source code in the src/test/java directory. You can create a package of test classes in that directory and create a new test class in the package

To create a test class for TestDemo, you can create files with the following structure

src
└── test
└── java
└── com
└── example
└── TestDemo.java

Introduction of demo test API

Get API

  • HOST: https://jsonplaceholder.typicode.com
  • API path: /posts/1
  • Request method: GET
  • Request Parameters: None
  • Request header: “Content-Type”: “application/json; charset=utf-8”
  • Request Body: None
  • Response status code: 200
  • Response header: “Content-Type”: “application/json; charset=utf-8”
  • Response body:
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}

Post API

  • HOST: https://jsonplaceholder.typicode.com
  • API path:/posts
  • Request method: POST
  • Request Parameters: None
  • Request header:“Content-Type”: “application/json; charset=utf-8”
  • Request Body:raw json format
  • Request Body:
{
"title": "foo",
"body": "bar",
"userId": 1
}
  • Response status code: 201
  • Response header:“Content-Type”: “application/json; charset=utf-8”
  • Response body:
{
"title": "foo",
"body": "bar",
"userId": 1,
"id": 101
}

Writing Test cases

  • Open the TestDemo.java file and start writing the test script.
  • The example script is as follows. For reference
package com.example;

import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
public class TestDemo {
@Test(description = "Verify that the Get Post API returns correctly")
public void verifyGetAPI() {
// Given
given()
.baseUri("https://jsonplaceholder.typicode.com")
.header("Content-Type", "application/json")
// When
.when()
.get("/posts/1")
// Then
.then()
.statusCode(200)
// To verify correct value
.body("userId", equalTo(1))
.body("id", equalTo(1))
.body("title", equalTo("sunt aut facere repellat provident occaecati excepturi optio reprehenderit"))
.body("body", equalTo("quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"));
}
@Test(description = "Verify that the publish post API returns correctly")
public void verifyPostAPI() {
// Given
given()
.baseUri("https://jsonplaceholder.typicode.com")
.header("Content-Type", "application/json")
// When
.when()
.body("{\"title\": \"foo\", \"body\": \"bar\", \"userId\": 1\n}")
.post("/posts")
// Then
.then()
.statusCode(201)
// To verify correct value
.body("userId", equalTo(1))
.body("id", equalTo(101))
.body("title", equalTo("foo"))
.body("body", equalTo("bar"));
}
}

Debugging test cases

  • Open the Terminal window for this project and run the test script by executing the following command
gradle test

Viewing Test Reports

Command Line Report

testng html Report

  • Open the project build/reports/tests/test directory.
  • Click on the index.html file to view the test report.

Maven version

See the demo project at https://github.com/Automation-Test-Starter/RestAssured-maven-demo

Initialize an empty Maven project

mvn archetype:generate -DgroupId=com.example -DartifactId=RestAssured-maven-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Initialization complete: After completing the wizard, Maven will create a new project directory and a basic Maven project structure

Configuration pom.xml

Add the following to the pom.xml file in your project

You can copy the contents of the pom.xml file in this project. For more information on configuration, please refer to the official documentation.

<!-- dependencies config -->
<dependencies>
<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.8.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- plugin config -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>

Configuration testng.xml

  • Create a resources directory under the src/test directory to store test configuration files.
  • Create a testng.xml file in the resources directory to configure the TestNG test framework.
  • For reference, the following is a sample configuration
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="restAssured-gradleTestSuite">
<test thread-count="1" name="Demo">
<classes>
<class name="com.example.TestDemo"/> <!-- test case class-->
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

initialization maven project directory

The directory structure can be found in » Project structure

Create a new test class in the project’s test source directory. By default, Gradle usually places the test source code in the src/test/java directory. You can create a package of test classes in that directory and create a new test class in the package

To create a test class for TestDemo, you can create files with the following structure

src
└── test
└── java
└── com
└── example
└── TestDemo.java

The api used by Demo

referable to » Introduction of demo test API

Writing Test cases

  • Open the TestDemo.java file and start writing the test script.
  • The example script is as follows. For reference
package com.example;

import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
public class TestDemo {
@Test(description = "Verify that the Get Post API returns correctly")
public void verifyGetAPI() {
// Given
given()
.baseUri("https://jsonplaceholder.typicode.com")
.header("Content-Type", "application/json")
// When
.when()
.get("/posts/1")
// Then
.then()
.statusCode(200)
// To verify correct value
.body("userId", equalTo(1))
.body("id", equalTo(1))
.body("title", equalTo("sunt aut facere repellat provident occaecati excepturi optio reprehenderit"))
.body("body", equalTo("quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"));
}
@Test(description = "Verify that the publish post API returns correctly")
public void verifyPostAPI() {
// Given
given()
.baseUri("https://jsonplaceholder.typicode.com")
.header("Content-Type", "application/json")
// When
.when()
.body("{\"title\": \"foo\", \"body\": \"bar\", \"userId\": 1\n}")
.post("/posts")
// Then
.then()
.statusCode(201)
// To verify correct value
.body("userId", equalTo(1))
.body("id", equalTo(101))
.body("title", equalTo("foo"))
.body("body", equalTo("bar"));
}
}

Debugging test cases

  • Open the Terminal window for this project and run the test script by executing the following command
mvn test

Viewing Test Reports

terminal report

testng html report

  • Open the project target/surefire-reports directory.
  • Click on the index.html file to view the test report.

More info

--

--

Nao

Test Automation Engineer, 11+ years of experience. Writing about Software Testing and Automation. My blog : https://naodeng.com.cn