Introduction
Rest Assured is one of the Java libraries which automate REST APIs. It’s not only easy to use but also flexible too, and supports HTTP methods such as GET, POST, PUT, PATCH & DELETE. Apart from supporting BDD (Behavior Driven Development), which has annotations i.e. @Given, @When, @Then, it can also be used with unit testing frameworks such as JUnit & TestNG and used with ‘Hamcrest’ framework for validation
Why do 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.
Advantages of Rest Assured
The following table lists some of the advantages of the library.
Advantages-1. It is open-source and hence free to use.2. It is very rich in syntax and ready-made assertions. Rest Assured requires less coding as compared to Apache HTTP Client.3. The setup of Rest Assured is easy and straightforward.4The response is given in JSON or XML format and is easy to parse and validate.5 It uses inbuilt Hemcrest Matchers for easy extraction of values.6 Response time is quick as also an assertion of status code.7. The library has a powerful logging mechanism. Also, we can verify headers, cookies, content type, etc on the fly.8 .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.9. It has very good support for various API authentication mechanisms.10 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.11 Rest Assured can also be integrated with Maven and CICD.12 It supports multi-part form data.13. Supports Spring Mock MVC, Spring Web Test Client, Scala, and Kotlin.14. 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.15REST 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.
How to Setup up Rest Assured.io with Eclipse
Step 1) Install Java. Refer to this guide
Step 2) Download an IDE to begin: eclipse
Step 3) Install Maven and set up your eclipse. Refer here.
Setup Rest Assured
Create a Maven Project in your IDE. We are using IntelliJ, but you will get a similar structure on any IDE you may be using.
Open your POM.xml
Add the below dependency to your POM.xml:
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.9.0</version>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20220320</version>
</dependency
<!-- https://mvnrepository.com/artifact/io.rest-assured/json-schema-validator -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>5.3.0</version>
Troubleshooting:
In case you see errors and not sure if the dependencies got downloaded well,
Perform a maven build to import all dependencies.
Still, you see errors, then do a maven clean followed by a maven install, and it should build without any errors.
You can add the below lines in your java class and see no compile errors are present.
import io. rest assured.RestAssured.*;
import io. rest-assured.matcher.RestAssuredMatchers.*;
import org. hamcrest.Matchers.*;
simple Rest Assured script
Given()‘Given’ keyword, lets you set a background, here, you pass the request headers, query and path param, body, cookies. This is optional if these items are not needed in the requestWhen()‘when’ keyword marks the premise of your scenario. For example, ‘when’ you get/post/put something, do something else.Method()Substitute this with any of the CRUD operations(get/post/put/delete)Then()Your assert and matcher conditions go here What will you fetch?
Open your browser and hit the URL In case you get an error on the browser,when you try to get a response for the request, See if you have used Https or Http. Your browser might have settings to not open insecure websites. See if you have any proxy or firewall blocks your browser from opening websites.
*Note – you did not use any headers here, no body, and no cookie. It was a URL and also you are getting content from the API and not posting or updating any existing content, so that makes it a GET call. Remember this to understand our first test better.
The Objective of your test:
The goal of the script is to print the same output on your IDE console as what you received on the browser through Rest Assured.
Key TakeAways
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.
Comments