Lead Marketing Strategist

An INFJ personality wielding brevity in speech and writing.

Reading Time : 0 Mins

API Testing With REST Assured

Lead Marketing Strategist

An INFJ personality wielding brevity in speech and writing.

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.

Leave A Comment

Related Posts

Senior Marketing Executive

Ameena Siddiqa is a seasoned marketer with hands-on experience in curating captivating content on the latest cloud, devops and enterprise technology trends. With a keen eye for emerging trends and a passion for storytelling, she has a knack for transforming complex concepts into engaging narratives that resonate with audiences across industries.

Senior Technical Manager - Business Intelligence

Kalyan Allam, a seasoned professional with more than 15 years’ experience, thrives on driving innovation and growth. His expertise lies in developing cutting-edge insights platforms using advanced visualization tools and integrating AI/ML capabilities. With a proven track record in building high-performing BI teams and fostering a data-driven culture, Kalyan excels in managing cross-functional teams and delivering impactful analytics solutions. Kalyan's thought leadership is evident through sharing BI best practices, case studies, and success stories.

Module Lead - Business Analyst

Preethy Senthilkumar is a seasoned IT professional with 12+ years of experience specializing in Business Analysis. She excels in aligning business needs with IT solutions, fostering collaboration across teams, and ensuring project success. With expertise in Agile methodologies, particularly within the Scrum Framework, she has worked across diverse domains including Investment Banking, Healthcare, and Postal logistics. Currently an Agile product owner at Zuci, Preethy is committed to client satisfaction in the Postal logistics industry. Certified as a Scrum Master and advanced Product Owner, she is pursuing a Master's in Business Analytics while also serving as a mindfulness instructor for Zuci's Happy Minds program.

Content Writer

Minna is a content developer specializing in software testing and Robotic Process Automation (RPA). She enjoys exploring the intricacies of cutting-edge software and knits comprehensible content that resonates with the audience. PS, she is a book lover.

Lead Marketing Strategist

A web-analytics nerd, speaker - here delving into (Big)-data.

Content Writer

Minna is a content developer specializing in software testing and Robotic Process Automation (RPA). She enjoys exploring the intricacies of cutting-edge software and knits comprehensible content that resonates with the audience. PS, she is a book lover.

Lead Marketing Strategist

A web-analytics nerd, speaker - here delving into (Big)-data.

Senior Marketing Executive

Ameena Siddiqa is a seasoned marketer with hands-on experience in curating captivating content on the latest cloud, devops and enterprise technology trends. With a keen eye for emerging trends and a passion for storytelling, she has a knack for transforming complex concepts into engaging narratives that resonate with audiences across industries.

Lead Marketing Strategist

A web-analytics nerd, speaker - here delving into (Big)-data.