Testing with rest-assured in 10 minutes

Testing is something that involves many activities, and one of these activities is testing APIs. Many tools and frameworks help increase the API test coverage, and one of the frameworks is Rest-assured.

If you are also curious to know other tools to test APIs, check these links:

Which tools are we going to use here?

  • IntelliJ IDEA
  • Gradle

Let’s start

Testing APIs with Rest-assured

Directory and files

Secondly, we need to install the libraries for the project to use Rest-assured. We will also install other libraries for the assertion and define the order of the tests.

Here is the link for the maven repository if you want to install different libraries: https://mvnrepository.com/

Thirdly, start coding.

Basic thoughts

Furthermore, it would be nice to know each case’s expected response beforehand, like the response the API will return for successful requests and not successful ones.

Expected response messages

List of endpoint responses

With these messages above, we can create different tests for each case.

Create your test class

Imports and first class code

List of endpoints

List of existent endpoints

Defining URL

Endpoint address

If only the status matter

Status and latency testing

The code above is covering the get employees endpoint in both latency and status code (200). Although you can also merge both checks, it is more understandable for this tutorial to be separated.

Starting from the most straightforward endpoint

List of employee’s ID

For comparing the structure of the response, we use schemas. The schemas are files with a specific configuration that tells how the response should be, and if it has attributes like ID, Names, etc., the schema specifies the types of those and which ones are required to have in the response.

For example, the following code block is the schema of the employee’s endpoint response above:

Employee list schema

Extra: To generate schemas, you can use any online site that transforms the response into a schema, create it yourself, or generate it with some tool, .e.g.: https://pypi.org/project/genson/.

Simple schema generator in python

Set your schema into the resources folder in your project because it enables you to refer to the file in any part of the code. The following example shows how to compare the response with the schema:

Resource folder
Comparing response with schema

Now you can execute it, but remember to turn up the rest API before triggering the test.

Executing the test method

It should give you a positive result.

Test status green

Creating and deleting employees

Database employee schema
Test create employee endpoint

And the rest of the attributes?

Employee created with only names set in the request. Response retrieved from: /employees/data/<id>

If you want to have all the fields fulfilled, you can copy and paste the following code to set the JSON object with the correct values:

All values for creating employee

Deleting employee

Checking the success message after deleting the last employee

Note: The path delete/last doesn’t have the ID number from the employee created. Instead, it searches for the last created employee and deletes it. It is a different endpoint created only for this tutorial example. The right endpoint for deleting with the ID number is delete/<id> , meaning that to delete employee ID number 14, it would be, delete/14. As you will see below, the endpoint path delete gets called for other testings.

Deleting employee with ID “TestFail” (String)
Trying to delete employee that doesn’t exists (-1 ID)

Schemas for everything

All schemas

So in your code, you could do something like this:

Comparing schemas for deleting successfully

Get employee data

Let’s call the endpoint and request ID number 3 like employees/data/3

The response above shows different data types for the attributes, like dates, numbers and texts. Therefore, the schema should declare these types accordingly:

Employee data schema

And you can repeat the comparison code showed in other examples but referring to the schema of employee data in the resources:

Getting the ID number 1 employee in the list

Covering other cases

Employee without the last na

What if instead of the date format “yyyy-mm-dd” using “dd.mm.yyyy”?

Different date format

Or a different value type?

Sending text instead of a number

And if we try to use any text instead of ID numbers for deletion and getting employees data?

Fail when trying to delete employee with invalid ID
Fail when trying to get employee’s data with invalid ID

Note: All the API responses listed are very generic for the purpose of this tutorial.

One more thing, ordering tests

For that, you can use the annotation Order.

Ordering tests

That is it! Congratulations for getting so far :)

Final notes

  • Rest-assured is super easy to understand and use.
  • It brings flexibility and productivity to the development and test process.
  • Everyone can use it, given its high readability regarding methods.
  • It has clear documentation and many examples on the internet.

Get the code here

Software nerd and data engineer enthusiast