What is REST Assured?

REST Assured is a Java library for validation of REST web services. It offers a friendly DSL (Domain specific Languages) that describes a connection to an HTTP endpoint and with their expected results. In REST Assured testing, we send a network request to our application, get a response back and compare it against a predetermined or expected result. Testing HTTP error codes becomes very useful when we want to make sure that our application behaves correctly, even when the input data is wrong.

REST Assured API Test Automation:-

Here we will automate REST API’s Post method using Rest-Assured library. We use Login create a post request. Use the following parameters in code.

APIUrl – Set URL variable of the Rest API

– Set URL variable of the Rest API APIBody – Set APIBody variable using setter with body of the Rest API parameters e.g. login.setApiVersion(“1.0”);

login.setActionName(“login”);

login.setEmail(“testingalertindia@gmail.com”);

login.setPassword(“123456”);

login.setMedium(“MANUAL”);

login.setAppKey(“myAppKey”);

– Set APIBody variable using setter with body of the Rest API parameters setContentType() – Pass the “application/json“, “application/xml” or “text/html” etc. headers to setContenType() method

Prerequisites:-

First of all we fulfill below mentioned prerequisites to Automate an API using REST Assured:-

Java should be installed Link

Rest Assured Java Jar / Maven Dependency

A sample Java project having HTTP/REST/JSON API implementations.

Rest Assured Maven Dependency:-

Rest Assured Maven Dependency Download Link

Sample Rest Assured Example:-

Given When Then Structure:-

Rest-Assured is using given/when/then notation.When you create BDD features, the majority of its scenarios will be made up of three main Gherkin keywords: Given, When and Then. Each one has a specific function:-

Given = a context or Getting a system in a particular state

= a context or Getting a system in a particular state When = something happens or Poke it

= something happens or Poke it Then=we expect some outcome or examine the new state

Test Case :-

Automate Login API and validate that the user logged in successfully having “Login successfully” returned JSON response message.

Test Script :-

Google, performs a GET call and makes sure that HTTP code 200 or success HTTP response is returned.

Gson is automatically used by REST Assured for JSON (de)serialization, as we will see in the examples

Validation :-

Verify the http response status returned.

returned. Verify that the response was successful which returned correct JSON.

Response Assertions:-

REST Assured also handles assertion within its library, the parsing of responses.

In the above example, we validated the output response message and generate response time. It provides various constructs for implementing assertions on cookies, response headers and response body. For doing assertions on response body it provides JsonPath for assertions on JSON responses and XmlPath for assertions on XML responses.

Other Rest Java Libraries:-

Jersey

Jackson

Spring REST web service using MVC

Axis

Jersey

Restlet

EasyRest

Conclusion:-

As a result we have automated tests for the REST API of a sample login application using the Java library REST Assured.

Share this: Tweet





Like this: Like Loading...