REST API allows two or more systems to communicate with each other through HTTP requests. We use the Java library Rest Assured for testing such RESTful APIs. It supports Behaviour Driven Development using Junit and TestNG frameworks with Selenium. As such, it is of particular relevance to testers for conducting automation of a web service or API. The following are some of the key concepts of Rest Assured API testing:
What is REST?
REST stands for Representational State Transfer. It is an architectural style for building web services that interact via an HTTP protocol. When a client request is made via a RESTful API, it transmits the information using one of many formats available-JSON, HTML, plain text, Python, PHP or XLT. Headers and parameters are used in these HTTP methods to send important identifier information related to authorization, cookies, metadata, caching and more.
What is Rest Assured?
Rest Assured is a widely used Java library used in the test automation of REST APIs.
It helps in writing scalable, efficient and optimized test cases for REST APIs automation.
It can be integrated with Junit and TestNG frameworks using the Given, When, and Then syntax.
It is an open-source tool and it integrates well with Maven, Allure report and Extent report.
It can test different HTTP requests like GET, PUT, POST, DELETE, and PATCH along with sending payloads and parameters in the request body and header information in the form of authorization cookies and more.
Rest Assured has inbuilt methods to verify and assert the response status and response codes of various requests sent to the server.
Rest Assured also has defined methods to verify the response body and extract messages from the response body.
What is the difference between URI, URL and URN?
URI—Uniform Resource Identifier
It is a string of characters used to identify a resource on the internet whether by location or time or both location and time. For example, to go and meet a person, we need both the name and address of the person.
URL—Uniform Resource Locator
It is a string of characters used to identify a resource on the internet by its location or address. We use this when we type www.websitename.com in the address bar to access some information.
URN—Uniform Resource Name
It is a string of characters used to identify a resource on the internet by its name. Since more than one resource can have the same name, it is ineffective to use URN to identify a resource.
So, URIs that specify the location are called URLs and URIs that specify the name are called URNs. Most of the time we use URL to identify a resource. The following is a structure of a URL:
Explanation of different parts of a Uniform Resource Locator:
https:-- this is called the medium or protocol
website.com- it is called the host or base URL and it is always the same.
After the URL and first slash—this is the resource-- to access that particular page, like a hotel, railways, flights, log in etc.
Query parameter—when we enter a query in the search box, the URL changes (it starts from the question mark) but the base URL remains the same. In this case, we are searching by flight name.
What are static imports in Rest Assured and why do we use them?
Using static imports in Rest Assured, we can directly use the methods given in a particular Class without specifying the name of the Class or creating an object of the Class. For example, instead of writing RestAssured.given().when()…, we can simply write do a static import of io.restassured.RestAssured.* and directly write given.when()….which is a bit simpler and easier to read. We can use the following static imports in Rest Assured:
What are the Given, When and Then methods used in Rest Assured?
Rest Assured is a Java library that supports Behaviour Driven Development in automating test cases. It uses the syntax of Given, When and Then in the following context:
Given()—We provide all the input details here, like, base URL, headers, path parameter, query parameter, and request body.
When()—We specify the resource, http request method like GET, POST, PUT, PATCH or DELETE.
Then()—It is used to validate the status code, status time, status line in some cases, response body, headers and messages which are part of the response body.
What is the method of chaining in Rest Assured?
Method chaining is a syntax for invoking multiple method calls in an object-oriented programming language. Each method returns an object and allows multiple calls to be chained together in a single line. For example, in Rest Assured the different methods of Given(), When() and Then() are held together with the help of dots. The syntax is as follows:
What is Request Specification in Rest Assured?
A group of tests may have some common specifications to create a Request. For example, the base URL, base path and some Header elements could be the same in multiple tests. In such a case we can use the RequestSpecification interface to group together common request specifications. To give a reference, we use the Given() method of RestAssured and write the code in the following manner to optimize it:
What are the various kinds of authentication methods available in rest Assured?
There are different forms of authentication methods available in rest Assured. Like, Basic authentication, Digest authentication, Form authentication and OAuth2. In Basic authentication, the user provides the authentication credentials like username and password. This is the most basic form of authentication. the syntax will be:
In Digest authentication, the API uses a key for transmitting the authorization. It does not require the password to be transmitted and, therefore, in terms of security is more secure than basic authorization. the syntax is similar to that of basic authorization:
In Form authentication, the credentials are passed in an HTML form through POST method. The syntax is :
In OAuth2.0, the user needs to generate an access token to authorize a user session. The syntax is as below:
How to perform response validations in Rest Assured?
Response is an interface available in io.restassured.response package. There are different methods available which can be used to validate the status code, status line, response time and content type. We can use getHeader(), getCookies() and getContentType() methods to get the values of the header, cookies and content type. We can then perform validation using Assert. assertEquals() method. The syntax for validation is as follows:
What is the difference between JSON and XML?
XML stands for extensible markup language like HTML. All XML data is a string, and it needs to be parsed. XML offers the capability to display data because it is a markup language. It supports various types of data display types such as numbers, text, images, charts, and graphs. It is not fully supported by the Ajax toolkit. As opposed to JSON, it supports namespaces. It supports various encoding standards and is more secure than JSON.
How to use POJO class in Java?
Plain Old Java Object programs are created to utilize the objects created in other Java programs. This class can have an arguments constructor and all objects defined must have some Getters and Setters values to access the objects by other Java programs. It is created in Java to increase the reusability and readability of the program.
Give an example of each GET, POST, PUT and DELETE method in rest Assured.
GET request fetches details of a particular resource from the server. Example of GET request:
POST request sends information along with parameters in the request body to create a new resource on the server. Example of POST request:
PUT request updates the value of a particular resource. Example of PUT request
DELETE request deletes a particular resource from the server. Example of DELETE request:
Writing automated tests at the API level with the help of Rest Assured provides increased stability and speed of execution. It provides value in terms of using different sets of data to do the testing of endpoints with multiple HTTP requests. being an open-source Java-based Domain Specific Language, it supports a Behaviour Driven Methodology of testing which allows it to be integrated with TestNG or Junit. This helps in timely report generation and setting up of the continuation integration and continuous delivery pipeline.