DEV Community

Cover image for First API Test Using REST Assured.
Hala Samir
Hala Samir

Posted on

First API Test Using REST Assured.

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?

Image description

  • 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

  • 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

And this is the displayed response:

Image description
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
The result:

Image description

Top comments (0)