top of page
  • Writer's picturekhyati sehgal

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.

rest_assured

rest_assured

Starting with a basic class that 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.

org.testng

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.

  1. 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.

  2. org.testng.annotations.BeforeMethod:– As the name states this is used to call a method before each method.

  3. 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.

  4. 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"));
}
}
else
{
RestAssured.port = Integer.valueOf(port);
}

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

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

In Configuration file, create 3 variables:

  1. base_port = 8086

  2. base_path =

  3. base_host = https://khyatisehgal.wordpress.com

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 the initial set up of service automation with java.  Will share more post in continuation to proceed with the working of code.

6 views0 comments
bottom of page