Journey Test Cases with Rest Assured

Yasin Onur Gürbüz
5 min readNov 21, 2021

--

Rest Assured is one in all the foremost standard libraries that is extremely utilized in API Test Automation in most companies. As the Trendyol Order Management System Team, we use REST-assured testing framework for our journey test cases. Requests like creating package, creating claim or cancelling an order are so ordinary cases for the order management system of an e-commerce company.

In this Rest Assured tutorial, I will try to give a brief explanation for Rest API, API Testing, API Automation and REST which we frequently use in our journey test cases.

First of all, let’s talk about the meaning of Api, REST-assured and JUnit.

What Is API & How Does It Work?

API stands for Application Programming Interface. It contains hard and fast of features that may be accessed and achieved with the aid of using some other software program system. Thus, it serves as an interface among special software program structures and establishes their interplay and records exchange.

You can think about the API like a restaurant’s menu. You can order a meal for your lunch on the menu. You don’t have to think about what is happening in the kitchen. You just order and wait for the food to arrive. API is like that, you just request it, then it gives the result.

Today, we will work on the API of PetStore which is https://petstore.swagger.io/ and we will understand the REST-assured requests.

What Is REST-Assured ? Which methods can be used?

Rest Assured allows you to check REST APIs with the use of java libraries and integrates nicely with Maven. With its enhances field matching techniques, RestAssured can easily obtain the expected results. Rest Assured has techniques to fetch statistics from nearly each a part of the request and reaction regardless of how complicated the JSON systems are.

For the trying out community, API Automation Testing remains new and niche. The JSON complexities maintain API trying out unexplored. However, that doesn’t make it much less critical withinside the trying out process. Rest Assured.io framework has made it quite simple the use of center java basics, making it a completely proper factor to learn.

Methods

It helps GET, POST, PUT, PATCH, DELETE, OPTIONS and HEAD requests and can be used to validate and affirm the response of these requests. Also it is able to be integrated with testing frameworks like JUnit, TestNG etc.

  • GET retrieves the resource at a specified URI.
  • PUT updates a resource at a special URI. Also be used to create a new resource at a special URI. Replaces the complete product entity.
  • PATCH support partial updates.
  • POST is used to send data to a server to create/replace a URI.
  • DELETE deletes a resource at a specified URI.

What Is JUnit ?

JUnit is a Java unit testing framework that’s one among the simplest test methods for regression testing. As an open-source framework, it is used to develop repeatable automated tests.

There is a banging list of Annotations to spot, execute, and support several options for the check strategies.Test execution, result verification and error detection can easily be handled with its efficient tools.

Configure REST Assured & JUnit

The other maven repositories such as org.slf4j, io.rest-assured compile, faterxml.jackson.core, google gson etc. used for the project. The repositories added to the github. The project github repository’s link will given at the end of the story. Generally, writing journey cases consists of four parts; creating a scenario, writing the test cases, base tests and clients.

First of all, we should create the scenario of the test case.

Scenario

Given : A user exists for the PetStore
When : Update the user information on PetStore
Then : User information is correctly updated

Secondly, writing the first class which the model of the request for user request creating. In addition, using getter and setter functions helps for the clean coding.

In order to comparing the result of the response, we should create the response of the fields. Then, in the test cases class, we call them for matching the fields.

Thirdly, the class of PetStoreTests for writing the test cases, the example of test case about the creating the user, then changing the information of the user, after a while, checking the updated user information gets true or not.

Furthermore, we set the account for the user, then updated it. The Assert That is a JUnit method from the Assert object that may be used to see if a given value matches an expected value. There are only two parameters that it accepts. The first is the true value, while the second is a matcher object. It will then attempt to compare the two and produce a boolean result indicating whether or not they are a match. Let’s look to the Base Test of the project.

Adding the client of PetStoreClient object for Rest Assured requests. Also, userUpdatedResponse for the Assert That method. Using that response gives us an opportunity for compare the fields.

Then, let’s look to the last part of project. It calls client of the Project.

In addition, If you want to give a header file you can add on the class of ReqSpecBuilder. You can add the informations of x-correlation-id, x-executor-user, x-agentname etc. Also, PETSTORE_API_URL is “https://petstore.swagger.io/” which is written in the config.proporties file.

In this example, using GET, PUT and POST requests for the user information.

HTTP response status code of “200-OK” uses in this example. However, there are a lot of different status code such as 201, 500, 504 etc.

To sum up briefly, REST-Assured

  • is a team of java libraries which allows us to automate Rest API checking out.
  • is Java-based, and expertise of core Java suffices for getting to know it.
  • facilitates fetch values of request and reaction from complicated JSON systems
  • facilitates set assert statements and conditions.
  • requests can be custom designed with an expansion of header, query, direction param, and any consultation or cookies to be set.

The project link is,

--

--