Rest Assured Framework

13 / Dec / 2021 by Sriyansh Jain 0 comments

What is API and API Testing?

  • API
    is abbreviated as Application Programming Interface. It is a computing interface that has a feature of transmitting and exchanging data between two separate software systems. While using applications like Facebook, Google, Twitter, Weather Forecast, we avail information via APIs. APIs are practiced to give access to data from outside the firewall.
  • API Testing
    is a type of software testing technique that is used to test the APIs directly — from its functionality to its performance, scalability, reliability, and security. API Testing focuses on receiving an exactly appropriate request and response in an application program.

REST vs. SOAP

  • SOAP
    The acronym for “Simple Object Access Protocol”, SOAP uses services interfaces to uncover the business logic. This protocol explains design to be strictly followed and needs much more bandwidth and resources than REST. It defines its own security and allows XML data format only. Generally, SOAP is less preferable than REST.
  • REST
    The acronym for “Representational State Transfer”, REST can make use of SOAP because it is a concept and can avail any protocol like HTTP, SOAP, etc. It aims to exhibit business logic and does not define too many standards like SOAP. REST requires less bandwidth and resources as compared to SOAP and it enables different data formats such as Plain text, HTML, XML, JSON ,etc. This is why REST is more popular than SOAP.

REST Assured

REST Assured is a type of library of JAVA used to test RESTful APIs. It is extensively utilized to test JSON as well as XML- based web applications. Moreover, it aids all methods including GET, PUT, POST, PATCH, and DELETE. Prerequisites needed are Java, Maven, TestNG, and IDE (IntelliJ, Eclipse, etc.) There are four types of HTTP methods – GET, PUT, POST, and DELETE. These requests can be mapped to CRUD operations as follows:

  • GET retrieves the depiction of the resource at an identified URI. GET should have no leftover reaction on the server
  • PUT updates a resource at an identified URI. PUT can also be utilized to generate a new resource at a specified URI if the server permits clients to specify new URIs
  • POST creates a new resource. The server allocates the URI for the new object and returns the same URI enclosed with the response message
  • DELETE is used to delete a resource at an identified URI

SYNTAX

The syntax of Rest Assured.io is given below. It follows Behavior Driven Development (BDD). BDD is an approach that defines the behavior of a feature characteristic through instances in plain text.

Code Description

  • Given()
    ‘Given’ is a keyword that allows you to set a background. You pass the request headers, body, query, path parameters, and cookies. This is elective if these items are not required in the request.
  • When()
    ‘When’ is a keyword that tells you the scenario. For example, when a user gets a request like get or post or put something or do something else.
  • Method()
    Replace this with any of the CRUD operations (get/post/put/delete)
  • Then()
    It includes the conditions like assert and matcher 

Advantages

  1. REST is open source
  2. It authenticates & justifies the response of the requests like “get”, “put”, “patch”, “delete”
  3. It provides a Given/When/Then test annotation, which immediately makes your tests simple enough to understand
  4. It supports CI/CD integration
  5. It can be used with any customized/open-source reporting tool
  6. It can be easily integrated with JUnit and TestNG frameworks

Disadvantages 

  1. It requires prerequisite knowledge of the Java programming language
  2. It does not support SOAP APIs explicitly
  3. It has an inbuilt reporting feature
  4. REST Assured is not purely thread-safe

Dependency

In general, dependency is an archive — JAR, ZIP , etc collected in the pom.xml file, inside of a <dependencies> tag

Creating a Get Request

The HTTP GET request is utilized to collect a resource from a server. The following example uses the get() method from the REST assured library.
The above code will result in the same response which was received when opening the same URL on a browser. This is how the response will be displayed in (Integrated Development Environment) IDE console window.

Parametrizing our Test

It implements the concept of Data-Driven Testing. RESTful API follows two types of parameters.

  1. Query Parameter
  2. Path Parameter
  • Query Parameter
    These types of parameters are appended at the end of the RESTful API endpoint.
  • Path Parameter
    These are the paths of the API endpoint.

REST Assured can control both types of parameters. Let us look at an elaborated sample script for this. For Query Parameter For Path Parameter Alternatively the param(), path parameters are determined using the pathParam() method.

Other features of REST Assured

REST Assured provides some additional features as well which are as follows – 

  • It can de-serialize POJOs. It permits you to serialize the properties and values related to a Java object instance directly into a JSON or an XML document, which can then be sent to a RESTful API using the POST method
  • Registering requests and responses. This can be largely useful when you need to examine API responses to create the applicable checks, or when you want to confirm that the request you are sending to an API is correct

Conclusion

  • Rest Assured is a very significant framework to write tests against a REST/hypermedia API
  • With its illuminating approach and the demonstrative method names, it grants one to easily acknowledge what a sure call is supposed to do and to return. JSONPath and Matchers grow the power and articulateness
  • Rest Assured is a collection of Java libraries that entitles us to automate Rest API testing
  • Rest Assured is a Java-based concept
  • It helps retrieves values of request and response from complicated JSON structures
  • The API request can be altered with a diversity of header, query, path param, and any session or cookies to be set
  • It helps set assert statements and conditions
  • Rest Assured is very attainable in the condition where the response is JSON type, its methods may not work consistently if the content type is HTML or plain text
FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *