First API Test Using REST Assured.
Tags: api, java, testing, beginners
Published: 2022-08-24 10:25:00

## What is REST Assured?
REST Assured is  a Java library that provides a Java DSL for simplifying testing of REST based services built on top of HTTP Builder.
It supports POST, GET, PUT, DELETE, OPTIONS, PATCH and HEAD requests.

**Advantages of REST Assured:**

- REST Assured  is an Open source.
- It provides easy parsing and validation of response in JSON and XML.
- REST Assured enables assertion for status code and response time,Headers, cookies, Content-Type, etc.
- It can be integrated with TestNG, J Unit,  Maven and CI/CD.
- REST Assured can be integrated with Selenium to create End-to-End test scenarios.
- REST Assured supports multi-part form data.
- REST Assured supports Spring Mock MVC, Spring Web Test Client, Scala and Kotlin.
- It can be used with Cucumber for Behavior Driven Development (BDD).


**Disadvantages of REST Assured:**

- REST Assured does not support testing of SOAP APIs explicitly.
- It requires good Java programming knowledge
- No inbuilt reporting. Serenity BDD is a good option here.

## Let's Get Started:
### What you need to start?

- Install Java.
- Install Maven
- Install IDE like VScode, Eclipse or Intellij.
- Create Java Maven project.
- Add REST-assured Maven dependency in pom.xml file.
https://mvnrepository.com/artifact/io.rest-assured/rest-assured/5.1.1
- Add TestNG dependency in pom.xml.
- We will use this API https://restful-booker.herokuapp.com/apidoc
- We will start with a simple request gets all bookings using a non BDD style.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ndg1mqjusz3gf2tff2s.png)

- Add a new class under test directory.
- REST-assured has method called baseUri(), the base URI that's used by REST assured when making requests if a non-fully qualified URI is used in the request
- REST-assured has a static overloaded method named get() which returns a reference of Response interface.
-The return type of REST-assured http methods is of type Response. This response contains all details returned by hitting request i.e. response body, response headers, status code, status lines, cookies etc.
ValidatableResponse is an interface. Response interface has a method named “then()” which returns ValidatableResponse, It can be used to validate a status code using statusCode() method.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7zp5l72m19h8wektsqgt.png)

- To view the response body in console, you can user .body() method for the response to view it. For Pretty-print method user for body() method to return the response as string.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bjlmxhv2ro4nhh4cu2y0.png)

And this is the displayed response:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tcwl21oixedizsqoc9rw.png)
And to log all the response details, you can user log().all() to view all the response details not only the response body.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ztn9s3pygv40nbnf93t2.png)
The result:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4m76wgst1lpnei6y6mnr.png)