API testing, Automation, Automation Framework, best practises, HTTP, HTTP methods, Java, java.lang.NullPointerException, rest assured, Services Testing, Testing, TestNG, Web Services, Web Testing

Part 1- API testing with rest assured.

In this post, I will share some basic rest assured code that can be directly used in a project which can support Java, Page Object Model, TestNg, API testing, etc.


Starting with a basic class which will have generic variables, imports and other methods to be called as shown here.

Imports to make:-

These imports are from different libraries like org.testng, io.restassured and other java required imports.


This is a test framework which has n number of utilities and inbuilt ready to use annotations in itself. I have used org.testng.Reporter, org.testng.annotations.BeforeMethod, org.testng.annotations.Optional and  org.testng.annotations.Parameters in this class.

  • org.testng.Reporter:- is for reporting, You need to set up a class to be called and this really depends whether you want to set reporting via testng or any other reporting mechanism.
  • org.testng.annotations.BeforeMethod:– As the name states this is used to call a method before each method.
  • org.testng.annotations.Optional:– This annotation can be called where you see the parameters can be optional for example while calling any rest service some arguments, parameters can be optional and non-mandatory so those can lie under this annotation.
  • org.testng.annotations.Parameters:- This annotation can be called for the parameters of rest service some arguments, parameters are the data providers which can be made configureable from the property file.
import static io.restassured.RestAssured.given;
import java.io.IOException;
import java.util.Random;
import org.apache.commons.lang3.RandomStringUtils;
import org.testng.Reporter;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import io.restassured.RestAssured;
import io.restassured.response.Response;

Setting variables

protected static String statusLine, body;
static long timeInMs, statusCode;
protected static Response response;
/* This class will configure all the properties required for the set up of rest assured.
* This will include host , port, cookies, auth token etc required to test any rest service.*/

Setting rest assured base, port and path

This method is the core which holds all the parameters required to set up rest assured.

Here, we are handling the case if any of the parameter falls under null or empty state then how shall the code will proceed.

// This method is picking properties like base, host, port from the property file. And setting that to the rest assured class.
@BeforeMethod(alwaysRun = true)
@Parameters({ "base", "host", "port" })

public static void restSetup(@Optional String r_base, @Optional String r_host, @Optional String r_port) {
String port = r_port;
String base_path = r_base;
String base_host = r_host;
if (port == null) {
if (!property.getProperty("base.isEmpty())
RestAssured.port = Integer.parseInt(property.getProperty("base_port"));
RestAssured.port = Integer.valueOf(port);

if (base_path == null)
if (!property.getProperty("base_path").isEmpty())
RestAssured.basePath = property.getProperty("base_path");
RestAssured.basePath = base_path;

if (base_host == null)
if (!property.getProperty("base_host").isEmpty())
RestAssured.baseURI = property.getProperty("base_host");
RestAssured.baseURI = base_host;

In Configuration file, create 3 variables:

In this way, we have created a Utility class in which we are setting up rest assured set up method. This method will be called every time before each method, and will pick property from config file hence making it configureable and dynamic.

Hope this will help in initial set up of service automation with java.  Will share more post in continuation to proceed with the working of code.

1 thought on “Part 1- API testing with rest assured.”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s