API Testing with REST Assured

API Testing with REST Assured

Testing everything through UI is complex, costly and time-consuming. With ever-growing need to support application on different devices and interconnected systems; a bare minimal three-tier architecture has become the de facto for application development in which the core business logic lies in the ‘Service Layer’. Hence, testing at Service level becomes important.

I am here to share my experience in using Rest-Assured for API testing to write powerful, readable, and maintainable automated tests for RESTful APIs.

What is REST Assured?

REST Assured is a Java Domain Specific Language API for simplifying testing of RESTful web services. REST Assured API can be used to invoke REST web services and match response content to test them. REST Assured can be used to test XML as well as JSON based web services. REST Assured can be integrated with JUnit and TestNG frameworks for writing test cases for our application.

REST Assured supports POST, GET, PUT, DELETE, OPTIONS, PATCH, and HEAD requests and can be used to validate and verify the response of these requests.

Highlights 

  • Supports JSON validator and JSON Path & XML Path to parse & verify response
  • Supports multi-part file upload and various authentication mechanisms
  • Easy Technical response data validation
  • Supports data-driven testing
  • Support Logging request & response details

Benefits 

  • Removes the need to write a lot of code that is required to set up an HTTP connection, send a request, receive a response and parse & validate
  • Supports BDD style Given/When/Then test notation, which makes the tests more readable
  • Being a Java library, easily integrates into a continuous integration / continuous delivery setup, especially when combined with a Java testing framework such as JUnit or TestNG

Rest-Assured flowchart

 

Sample XML Post request with Rest-Assured

Here is a sample program that posts request with XML payload.

XML payload saved in a file is read and serialized as payload. Log().all() function is used log all the request specification and response. AssertThat().statusCode() is used to assert the status code of the response received.

import static io.restassured.RestAssured.given;

import io.restassured.http.ContentType;

import io.restassured.response.Response;

 

public class RestAssured {

public Response apiResponse;

 

public boolean postXmlData(String ApiURL, File xmlFile) throws IOException {

boolean getResponse = false;

 try {

 

Path xml_Path = Paths.get(xmlFile);

byte[] xmlData = Files.readAllBytes(xml_Path);

String myRequest = new String(xmlData, “ISO-8859-1”;

apiResponse = given().log().all().

contentType(ContentType.XML).

accept(ContentType.XML).

body(myRequest).

when().

post(ApiURL);

 

apiResponse.then().log().all().

assertThat().statusCode(200);

getResponse = true;

} catch (AssertionError e) {

e.printStackTrace();

}

return getResponse;

}

}

Sample XML response parsing

Let us consider the response received is in the following format:

<?xml version=”1.0″ encoding=”UTF-8″?>

<response>

<empdetails>

<id>Z-123</id>

<uname>awalker</uname>

<firstname>Alan</firstname>

<lastname>Walker</lastname>

<email>awalker@testmail.com</email>

</empdetails>

</response>

 

Following sample program parses the response using xmlPath(), sets the root to ‘response.empdetails’ and can get any of the node value

public String getValueFromXmlResponse(String rootNode,String getNodeValue) {

String nodeValue = null;

 try {

nodeValue =apiResponse.xmlPath().setRoot(rootNode).getString(getNodeValue);

 

} catch (AssertionError e) {

e.printStackTrace();

}

return nodeValue;

}

 

REST Assured offers a wide range of other useful features with a short learning curve. With proper single goal wrapper methods, one can create powerful automated tests for RESTful APIs with Rest-Assured easily. From our experience in using different tools for API testing, we see Rest-Assured as a perfect fit for automation projects when the preferred tool stack is Java – OpenSource

 

About Zuci
Zuci is revolutionizing the way software platforms are engineered with the help of patented AI and deep learning models. Learn more about Zuci at www.zucisystems.com

About the author

Priyesh is the Lead Software Development Engineer in Test. He is extremely passionate about coding and one of the fastest we have known. Find him at Priyesh.

By |2018-10-11T11:23:32+00:00October 11th, 2018|Tech Stories|0 Comments

Leave A Comment