Automate your API Test Cases Using REST-ASSURED

Reading Time: 5 minutes

Here, we’ll discuss about how can we automate our testing of RESTful webservices using REST ASSURED

Never used Rest-Assured before? That’s not a problem because today I’ll explain right from the scratch. As a result, that beginners can also start their journey of testing APIs using Rest-Assured

Today we’ll cover following topics:

  • What is Rest Assured?
  • How does it works?
  • How to create or setup a project?

Let’s get to our first question

What is Rest Assured?

In brief Rest Assured is nothing but a JAVA library or a set of JAVA APIs for testing and validating RESTful webservices. So,it simplifies the testing of REST based services built on top of HTTP Builder. Above all, REST-assured supports the different REST requests and can be used to validate and verify the responses from the APIs.

Testing and validation of REST services in Java is harder than in dynamic languages such as Ruby and Groovy. In other words, REST Assured brings the simplicity of using these languages into the Java domain. Hence you can simply create to test suit using JAVA.

Interestingly it supports both XML and JSON based webservies.

Now, there are lot and lots of functions and classes which we can use to creating a test for a webservices. So, I’ll explain you under project setup.

How Rest-Assured works?

Rest-Assure supports all the HTTP methods like post, put, get, patch, options, delete, head, etc.

For instance, it also have methods for including basic and preemptive auth, OAuth and OAuth 2.0 in your requests.

So by using Rest-Assured you can simply :

  1. First, Create a Request
  2. Then,Send a Request using any HTTP method as per the requirement
  3. Finally, Assert the Response of your Request

How to create or setup a project ?

in the first place create a maven project and include the following dependencies in the pom.xml file [Some of these will be used in our next blog]
<!-- -->
<!-- -->
<!-- -->
<!-- -->
<!-- -->
<!-- -->

<!-- -->
<!-- -->
<!-- -->
<!-- -->

by the time you’re all set to start framing our Test class

In order to do the same, just follow the steps as shown below

  1. Define some static variables which we’ll require in next step:
static RequestSpecification request;
static int status_code;
static ExtentTest logger;
static ExtentReports extent = new ExtentReports();
  1. Next step is to initialize our reports.
	public static void reportsInitialization() {
	ExtentHtmlReporter reporter = new ExtentHtmlReporter("./Reports/AllRequest_Extent_Report.html");
		// Environment Setup
		extent.setSystemInfo("OS Name", "Windows");
		extent.setSystemInfo("Username", "Aman Saxena");
//Provide the title and description of you test
		logger = extent.createTest("***API Test***", "All API methods will be tested here");

important to realize: Wherever you need log any information in your extent report, use methods like, logger.pass(“message”),“message”), logger.warning(“message”), etc.

  1. Setup a generate method to dispose off your reports
	public void generateReports() {

So, Above all, are the step by which we can setup and generate out extent reports.

Now let’s move to our main method.
  1. Set up your POST request

For instance, there are two types of payloads/method bodies which we can use under the post method:

a. Simple payload
b. Complex payloads

Firstly, let’s discuss both of them but start with the simple one

a. Simple Payload : In short, these are the object written in simple key/value pairs without having any nested array and something like that.

for example:

	// create a json object
		JSONObject jobj = new JSONObject();
		jobj.put("id", 14);
		jobj.put("title", "RestAssured");
		jobj.put("author", "Test User");
		// parse the json object into the request body
		// Json string generated here will be like 
                {"id" : "14",
                 "title" : "RestAssured", 
                 "author" : "Test User"   

b. Complex Payload : These are the object having nested objects or array of objects. For creating complex payload we can use POJO classes. like below and if you don’t know about those then I recommend you to learn about that first from

another key point, just create the following POJO classes in your project.
Ad Class:
public class Ad { String company; String url; String text; public Ad(String company, String url, String text) {; this.url=url; this.text=text; } public String getCompany() { return company; } public void setCompany(String company) { = company; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getText() { return text; } public void setText(String text) { this.text = text; } }
Data Class:
package utils; public class Data { String email; String first_name; String last_name; String avatar; public Data( String email, String first_name, String last_name, String avatar) { = email; this.first_name = first_name; this.last_name = last_name; this.avatar = avatar; } public String getEmail() { return email; } public void setEmail(String email) { = email; } public String getFirst_name() { return first_name; } public void setFirst_name(String first_name) { this.first_name = first_name; } public String getLast_name() { return last_name; } public void setLast_name(String last_name) { this.last_name = last_name; } public String getAvatar() { return avatar; } public void setAvatar(String avatar) { this.avatar = avatar; } }
PatchPayload Class:
package utils; public class PatchPayload { String id; String email; String password; Data data; public String getEmail() { return email; } public void setEmail(String email) { = email; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getId() { return id; } public void setId(String id) { = id; } public Data getData() { return data; } public void setData(Data data) { = data; } public Ad getAd() { return ad; } public void setAd(Ad ad) { = ad; } Ad ad; public PatchPayload(String id,String email, String password, Data data, Ad ad) { = id; = data; = ad;; this.password=password; } }
SendPayload Class:
package utils; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY) public class SendPayload { static String finalPayload; public SendPayload(String id, String email, String password, String first_name, String last_name, String avatar, String company, String url, String text) { if (text.isEmpty()) { text = "Here we can provide text description what ever we want"; } Data d = new Data(email, first_name, last_name, avatar); Ad a = new Ad(company, url, text); PatchPayload p = new PatchPayload(id, email, password, d, a); ObjectMapper o = new ObjectMapper(); try { finalPayload = o.writerWithDefaultPrettyPrinter().writeValueAsString(p); } catch (JsonProcessingException e) { e.printStackTrace(); } System.out.println(finalPayload); } }
  1. Create an anonymous object of SendPayload and pass necessary information in the constructor.
new SendPayload(id, "","abc@123", "Test", "User", "avatar.jpg", 
                "ABC pvt ltd", "", "");
  1. Now, call the finalPayload method to generate a payload.


  1. Simply, call post( ) method of Rest Assured and provide the required assertions
status_code =;
		if (status_code == 201) {
		logger.pass("Data successfully posted to the server");	
                else {
	      "Failed post request");

In the same way, let’s write some code for GET method as I’ve written below:

/////////////////GET METHOD\\\\\\\\\\\\\\\\\\\\\\\\\\\ request = RestAssured.given().baseUri(baseURL).basePath(“/api/users?page=2”).contentType(ContentType.JSON); status_code = request.get().statusCode(); if (status_code == 200) { logger.pass(“Data successfully fetched from the server”); } else {“Failed get request”); }

In addition, you can write code for to get message body, status line etc.;
Lastly, call the generateReports() method to end our program and generate reports.;

Finally, run this program as Java application.

In addition to above, check for the Extent Report at the path you have provided and open it in your browser. Hence, It will look something like this.

So, to conclude that’s all for this topic . Above all, are the simple steps by which you can create and automate your APIs using Rest-Assured

Unquestionably, you can use below mentioned reference for further information

what’s more: