Reading Time : 0 Mins

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

Software QA

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:

Z-123

awalker

Alan

Walker

awalker@testmail.com

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

Looking to improve your API’s functionality? Take a look at Zuci’s API Testing services and see how you can leverage Zuci for your business needs.

Keerthika
Keerthi Veerappan

An INFJ personality wielding brevity in speech and writing. Marketer @ Zucisystems.

Share This Blog, Choose Your Platform!

Leave A Comment

Related Posts