top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-
Writer's pictureArti Shankar

API Automation using Rest Assured in Simple Steps


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?

  1. It is an open-source library and has an active development community making it a great choice for API automation.

  2. Earlier, we had to use dynamic languages like Ruby, Groovy for API testing and it was quite challenging.

  3. Validation and testing of REST services are harder in Java. Using REST Assured, it becomes simpler and easier.

  4. 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.

  1. It does not support the testing of SOAP(Simple Object Access Protocol) APIs explicitly.

  2. Using this library requires that the user has good Java programming knowledge

  3. 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

  1. 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.

  2. 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,

  1. Perform a maven build to import all dependencies.

  2. Still, you see errors, then do a maven clean followed by a maven install, and it should build without any errors.

  3. 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()‘Givenkeyword, 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:

  1. It is an open-source Java library and is free to use.

  2. It is used for validating and testing Java APIs.

  3. Apart from other features like Java DSL syntax, also supports all HTTP methods and REST methods like GET, POST, PUT, DELETE etc.

  4. We can integrate Rest Assured with all major automation frameworks like TestNG, JUnit as well as integrate it with maven and CI/CD.

  5. 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.







195 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page