Automating API tests with REST Assured -1

Lakmali Bandara
5 min readMay 9, 2020

What is API

API or “Application Programming Interface” is an interface between two software systems, which provides interaction and data sharing. The basic principle of API is to send requests to a server and receive a response, indicating to the sender whether the request was successful or not.

Response codes

In the case of a request, a reply from the server is received in the form of a response code, which gives an indication of the type of response that it is. In general, there are more than 60 possible response codes, but some of them are more common than others, for example:

200 OK
201 Created
204 No Content
304 Not Modified
400 Bad Request
401 Unauthorized
403 Forbidden
404 Not Found
500 Internal Server Error

These response codes are designed so that the first number of each code corresponds to a certain informative group, which reflects the type of response it has.

1xx Informational 2xx Success 3xx Redirection 4xx Client error 5xx Server error

API call methods

API call methods define what type of request will be sent. Whether for it to be a new addition of data to a platform or an update to already existing data. Just like with response codes, there’s a multitude of call methods, with the most popular being:

  • GET method obtains information from the corresponding endpoint without requiring input data. This method can receive just a response code, or a response code and response body, containing data that was asked to be returned.
  • POST method is used to send new information to the server, or to update already existing information, by entering the requested information in the request body before sending it. Most often, this method is used to create new entries in an existing database.
  • PUT method is used only to update existing data and returns a positive result only if the system contains already existing and updatable data.
  • PATCH method is similar to PUT, in that it is used for updating existing data, but differs from it in so that to use it, it does not necessarily indicate that the user has to input all data of the database record. It is enough to specify the desired data which needs to be updated.
  • DELETE method is self-explanatory. It’s used to delete existing data from the system. This option is non-reversible.

Now you have a question about what is different of POST, PUT and PATCH .

GO through the example overview
For every client data, we are storing an identifier to find that client data and we will send back that identifier to the client for reference.

POST

  • If the client sends data without any identifier, then we will store the data and assign/generate a new identifier.
  • If the client again sends the same data without any identifier, then we will store the data and assign/generate a new identifier.
  • Note: Duplication is allowed here.

PUT

  • If the client sends data with an identifier, then we will check whether that identifier exists. If the identifier exists, we will update the resource with the data, else we will create a resource with the data and assign/generate a new identifier.

PATCH

  • If the client sends data with an identifier, then we will check whether that identifier exists. If the identifier exists, we will update the resource with the data, else we will throw an exception.

Note: On the PUT method, we are not throwing an exception if an identifier is not found. But in the PATCH method, we are throwing an exception if the identifier is not found.

When is API testing useful

One of the API’s advantages is that testing can start even before the creation of a UI, allowing for early testing, as the tester does not have to wait until the programmer creates UI elements for each functionality. API tests both manually and automated run much faster because the tester physically does not need to go through the UI, repost test data. All it takes is a single or multiple, API calls to be performed to successfully test functionality.

What is REST

REST or “Representational State Transfer” is a type of architecture created as a guideline for computer systems. It determines things like customer-server relationships but does not save client contexts on a server between requests so that the client can communicate with the server without knowing previous requests, unified guidelines, etc. Most commonly used for HTTP.

What is REST Assured?

REST Assured is simple java library that is used to test REST services and create an automated script. It can be used for regression testing of REST services or one can use it for smoke testing, sanity testing depends on requirement.REST Assured is open-source, which makes it easily accessible to everyone, therefore, becoming one of the most popular REST API validation tools. This library has been developed by Jayway.

Why REST Assured?

There are many reasons for choosing REST Assured for testing REST services. With all the below features one can handle automated REST API testing much easier.

  1. It provides simplicity like dynamic languages such as Ruby and Groovy.
  2. REST Assured working as the black box while testing one’s application.
  3. It supports all classic test runners such as JUnit or TestNG.
  4. We can write tests for the application written in any language such as Python,Ruby,Java,.Net ETC.
  5. Rest Assured is free to use and there is no explicit license required to use.
  6. It supports scala and groovy thus it can be used by various developer/QA who knows these various languages.
  7. Rest Assured can be used as Maven dependency.
  8. It supports POST, GET, PUT, DELETE, OPTIONS, and HEAD.
  9. It provides DSL-Like syntax.
  10. If someone needs to perform JSON Schema validation than it supports these and this is one of the cool features and provides basic validation.
  11. It provides Specification Reuse.
  12. Rest assured also provides easy file upload so one need not worry about this action and don’t need to write complex java code anymore.

Will continue with technical part — Refer part 2

--

--

Lakmali Bandara

Technical Lead Quality Engineering | Framework Developer | API Tester | Blogger