How To Use Rest Assured for Automating API

Posted By : Sumita Goswami | 31-May-2021

Rest Assured

In brief Rest-Assured is basically nothing but a JAVA library or we can say it is a set of JAVA APIs for testing, verifying, and validating RESTful web assistants. So, it clarifies the testing of REST established assistants assembled on top of HTTP producers. A top all, REST-Assured backing of the contrasting REST requests can be passed down to approve and verify the responses from the APIs.

Testing and approval of REST services in Java are most difficult than in progressive languages such as Ruby and Groovy. In other words, REST Assured carries the directness of using these languages into the Java sphere. 

How does the Rest-Assured tool work?

Rest-Assure accepts all the HTTP methods like the post, put, get, patch, options, delete, head, etc.

For detail, it also has methods along with basic and deterrent auth, OAuth, and OAuth 2.0 in our requests. With the use of  Rest-Assured we can simply :

  1. First, Create a Request
  2. Then, Send a Request with the use of any HTTP method as per the need:
  3. After that, Assert the Response of the Request
  4. There are diverse reasons to choose the Rest-Assured tool; Some reasons are as follows:
  5. Open source  so, it is free of any license cost
  6. Established on Java

Overture abutment for all kinds of HTTP approaches like POST, GET, PUT, DELETE, OPTIONS, PATCH, and HEAD

Abutment the BDD/Gherkin behavior of writing, which abutments clear coding & appreciates readability.

Implements effortless alliance with exemplar testing & covering frameworks such as TestNG and JUnit runner.

Users can test an application drafted in any language like Java, .Net, Python, Ruby, etc.

JSON schema validation can be Achieved by the Rest-Assured tool.

Accepts GPath to go over through result, which is a brilliant fit for both XML and JSON result abnegation.

Accepts both path and query params.

Composes it easy to test and validate REST employments in the Java domain which is commonly difficult.

How We Can Execute API Testing Using REST Assured

Setup:

Formerly the Maven project is Build, to be able to use REST Assured in tests, we need to add dependencies in Project Object Model (POM) files:

A good  technique is to use static imports in class files, so it turns out coherent:

Response verification and validation

1. Authenticating the status code

2. The most elementary and mandatory validations one can do with REST Assured Result is to authenticate status code, which can be done by using the status code() method on an acknowledged object. An easy example to achieve the status code and assert across a conventional value is:

i.then().statusCode(200);

ii.int statusCode = rawRes.statusCode();

3..a ssertThat()

      .statusCode(HttpStatus.OK.value());

4. Add the header parameters they can be: (single header/multi header) in the request

Manytime web services require headers to be passed as input to the call, without which the call will not be successful. So, we pass single header

@Test

.given()

.baseUri(host)

.header(“Authorization”,obtainAccessToken())

.extract()

.response();

Multiple headers (which get merged by default) or headers containing multiple values they can also be passed:

given().header(“header1”, “value1”, “value2”). ..

given().header(“abc”, “1”).header(“xyz”, “2”).

5. Set query parameters

The query parameter in a URL always comes after the symbol “?”, as shown below

https://abcd.efgh.com?fromDate=toDate=

6. Setting path parameters

Path parameter is a part of the URL and it  is used in calls on the web service. One of the ways we can use path parameters is by applying variables in the following example. PayrollId can have  any effective variable that is passed in the POST HTTP call:

@Test

given()

.baseUri

.header(“Authorization”,obtainAccessToken())

.contentType(ContentType.JSON)

.payload.put(“abcd”,0)

.and()

.when()

.post(“https://myblog.io/payroll/{payrollId}”, payrollId)

.

.

.extract()

.response();

7. Set content type of request

The data that is goes to the server in a POST request or the data that is coming from the server as a GET request most likely has a content type. The type of body, be it XML, JSON, TEXT, or some other type,  is always known by the Content-Type header.

If a POST request has JSON data then the Content-Type header will have a value of application/JSON or can be stated as enum ContentType.JSON. correspondingly, for a POST request containing XML, the Content-Type header value will be application/XML or can be specified as enum ContentType.XML.

Complete example of Automating GET Api by using Rest-Assured:

package com.oodles.accounts;

@Test

public String obtainAccessToken() {

Map<String, String> response = 

Post("https://abcd.efgh?email=abcd@efgh&password=12345&rememberMe=false").as(Map.class);

String accessToken = response.get("accesstoken");

return "Bearer " + accessToken;

}

@Test

void test_01() {

given().header("Authorization",obtainAccessToken())

.get("https://abcd.efgh?year=2021&month=")

.then().statusCode(200);

Conclusion:

REST Assured prepares to test easy with the help of REST APIs and smoothly integrates JUnit and TestNG.

 

About Author

Author Image
Sumita Goswami

Sumita is a certified software tester with skills in manual testing. She believes in continuous career growth by learning new things everyday.

Request for Proposal

Name is required

Comment is required

Sending message..