In our previous articles, we have discussed the basics of the REST approach including familiar terms, architectural elements, etc. Moving one step ahead in this direction, let us go ahead with API testing using REST in this article. The most popular technology we use for API testing is "Rest Assured". We will cover the following topics related to REST Assured in this article:
- What is Rest Assured?
- Why do we need it?
- Latest Versions.
- Advantages of Rest Assured.
- Disadvantages of Rest Assured.
What is REST Assured?
Rest-Assured is an open-source Java-based library useful for testing and validating REST APIs or RESTFul Web Services. It is like a headless (no GUI) client that accesses and tests the REST Web Services. It was developed and maintained by Johan Haleby with the support of several other contributors.
Rest assured simplifies the testing of REST-based services. It brings out the simplicity of dynamic languages like Groovy and Ruby that perform API testing in Java. The library supports any HTTP method and also has explicit support for GET, POST, PUT, DELETE, OPTIONS and HEAD. It also includes specifications and validation like parameters, headers, cookies, etc. We can also use it to verify and validate the response of the HTTP requests.
Apart from testing JSON-based web services, Rest Assured can also be used to test XML-based web services. We can also integrate this library with JUnit and TestNG frameworks and write test cases for applications. Further, it can be integrated well with Maven and its efficient matching techniques produce straightforward results.
Yet another powerful feature of REST assured is its support for XML Path and JSON Path syntax to check specific elements of the response data similar to using XPath API. For people new to such concepts, please refer to the following syntax examples.
<?xml version="1.0" encoding="UTF-8"?>
<title>Freedom In Exile</title>
JSON path syntax:
Apart from all the above features, this fantastic library also provides various other features like DSL-like syntax, Specification Reuse, XPath-Validation, easy file uploads, and all the features that are conducive to automated testing.
Why do we need Rest Assured?
The above discussion about this java library convinces us that it is a reliable library. But why do we need it or what are the reasons to use it in our application testing?
Following are the main reasons we need Rest Assured :
- It is an open-source library and has an active development community making it a great choice for API automation.
- Earlier, we had to use dynamic languages like Ruby, Groovy for API testing and it was quite challenging.
- Validation and testing of REST services are harder in Java. Using REST Assured, it becomes simpler and easier.
- This library uses Java and therefore it becomes simple to send HTTPS requests with customizations using basic Java code. Once we know the basics of API and integration testing, automation using Rest Assured gives good confidence on the backend. Thus we can focus more on front-end testing.
Following are some of the versions of the Rest Assured library we use generally in applications.
- io.rest-assured - This is a Java DSL for easy testing of REST services. The last release: 11, December 2020 (4.3.3).
- com.jayway.restassured - This is a legacy library and used for easy testing of REST services using Java DSL. Last Release: 4 March 2016.
- ddf.thirdparty - This is a third-party library. Last Release: 09 April 2021
Advantages of Rest Assured
The following table lists some of the advantages of the library.
|It is open-source and hence free to use.
|It is very rich in syntax and ready-made assertions. Rest Assured requires less coding as compared to Apache HTTP Client.
|The setup of Rest Assured is easy and straightforward.
|The response is given in JSON or XML format and is easy to parse and validate.
|It uses inbuilt Hemcrest Matchers for easy extraction of values.
|Response time is quick as also an assertion of status code.
|The library has a powerful logging mechanism. Also, we can verify headers, cookies, content type, etc on the fly.
|It can easily be integrated with other Java libraries like TestNG, JUnit, etc. We can also integrate it with Selenium-Java and achieve end-to-end automation.
|It has very good support for various API authentication mechanisms.
|It supports JsonPath and XmlPath that helps in parsing JSON and XML response. It also has support for the JSON Schema Validation library to verify JSON Schema.
|Rest Assured can also be integrated with Maven and CICD.
|It supports multi-part form data
|Supports Spring Mock MVC, Spring Web Test Client, Scala, and Kotlin.
|It follows the BDD (Behavioural Data-Driven) approach and keywords like given() when(), then() which makes code readable and supports clean coding. This feature is available from version 2.0.
|REST Assured 4.1.2 adds support for Java 13.
Disadvantages of Rest Assured
The library has the following disadvantages.
- It does not support the testing of SOAP(Simple Object Access Protocol) APIs explicitly.
- Using this library requires that the user has good Java programming knowledge
- There is no inbuilt reporting in Rest Assured.
We have the following takeaways from this post:
- It is an open-source Java library and is free to use.
- It is used for validating and testing Java APIs.
- Apart from other features like Java DSL syntax, also supports all HTTP methods and REST methods like GET, POST, PUT, DELETE etc.
- We can integrate Rest Assured with all major automation frameworks like TestNG, JUnit as well as integrate it with maven and CI/CD.
- The library has a rich syntax and since it is open-source more and more functionality keeps on adding which makes it a very efficient and simple library for API automation.
With this knowledge, we will begin with actual API testing tutorials in our subsequent articles. Before that, we will discuss the configuration of Rest Assured with Eclipse IDE.