API Testing with Rest Assured

Didem TEMEL
5 min readApr 23, 2022

--

Hi there, this is my first blog post and I will be sharing my experience on API testing with Rest Assured.

Rest Assured is a technology that allows us to write our API tests in Java. It supports requests such as GET, POST, PUT, PATCH, DELETE, OPTIONS and HEAD and can also be used to validate and verify their responses. For example, we can verify status code, header or body response.

Today, I am going to use the Rick And Morty API with three main controllers which are Episode, Character, and Location. In my project, I will focus on the Character controller.

Let’s create a Maven project and add the dependencies to the pom.xml file.

<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.3.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.5</version>
<scope>test</scope>
</dependency>

I am going to write tests for Get a single character and Get multiple characters endpoints. I will start by creating classes listed below;

  • ReadableResponse
  • BaseService
  • RickAndMortyService
  • CharacterResponse
  • ErrorResponse
  • BaseTest

ReadableResponse: I have created a generic response class to use in BaseService and test classes. We will be able to convert this readable response to different objects depending on the needs of our tests. I used @Data annotation which is a convenient shortcut annotation that bundles the features of @ToString, @EqualsAndHashCode, @Getter / @Setter and @RequiredArgsConstructor together. To use Lombok, we should add Lombok dependency in pom.xml.

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>

BaseService: I used RequestSpecification which is an interface that allows you to specify how the request will look like. This interface has readymade methods to define base URL, base path, headers, etc. I used Given(), When() and Then() syntaxes of Rest Assured which are BDD like and easy to understand. Test cases have common specifications to create a request, I write BaseService to not rewrite these specifications so that I can keep my code clean. We can pass parameters in methods based on method’s needs. For example, to use getRequest method we should pass endpoint as a string.

RickAndMortyService: This service extends the BaseService since it contains a getRequest method. I keep the service urls here and write multiple methods for each endpoints in the Character controller.

CharacterResponse: Character object is used to map responses which have returned from endpoints. In this object class, I used Lombok which generates getters / setters and handles them in the background for all variables marked with @Getter / @Setter.

Since the Location and Origin values in CharacterResponse class are also objects, I have created Location and Origin models.

ErrorResponse: In my test cases, I do not only write happy paths, I also write test cases for scenarios such as 400, 401, 404. I will validate error messages which are returned from the endpoint.

BaseTest: I have created BaseTest class under Test directory. I will add the methods that I will use in all test classes here. For example, I added rickAndMortyService method.

I need to have a good understanding of the project before starting to write tests. Therefore, I am going to use Postman to explore endpoints. After that, I am going to write my scenarios and start coding.

Test cases for Get a single character endpoint

Scenario 1:

Go to the Get a single character endpoint
Request character with id 2
Assert that status code is 200
And assert that character 1 is Morty

Scenario 2:

Go to the Get a single character endpoint
Request character with id 999
Assert that status code is 404
And assert that error message is “Character not found”

I have created GetASingleCharacterTest class which extends BaseTest. The endpoint we will be using requires an id. I am going to define id as a static variable. Then I will call the getASingleCharacter method in my rickAndMortyService service and send a request by passing it an id. The result will return as a readableResponse object. For the first scenario, I am mapping this object to the CharacterResponse object and for the second scenario I’m mapping this object to the ErrorResponse object. After that, I will do the validations.

Test cases for Get multiple characters endpoint

Scenario 1:

Go to the Get multiple characters endpoint
Request characters list with ids 1 and 2
Assert that response has two characters
And assert that status code is 200
And assert that character 1 is Rick
And assert that character 2 is Morty

Scenario 2:

Go to the Get multiple characters endpoint
Request characters list with ids 1 and 999
Assert that response has one character
And assert that status code is 200
And assert that character 1 is Rick

Scenario 3:

Go to the Get multiple characters endpoint
Request characters list with ids 998 and 999
Assert that response has no character
And assert that status code is 200

I have created GetMultipleCharactersTest class which also extends BaseTest. The endpoint we will be using requires a list with ids. I am going to define list of ids. Then I will call the getMultipleCharacters method in my rickAndMortyService service and send a request by passing it an id list. The result will return as a readableResponse object. For the scenarios, I am mapping this object to the CharacterResponse array. After that, I will do the assertions such as validating status code, response’s length and character response.

After creating test classes, I created TestNG XML file called test.xml. TestNG allows us to run tests in parallel and execute multiple test cases for various Java files. I have used < suite > , < test > and < classes > tags. Suite is where multiple test classes can be placed. For example, we have two test classes.

The next step is to run the project using the XML file. Right-click on the test.xml file and run the file. The output after the execution is shown below.

If you want to take a look at my project, its GitHub link is shared below.

In this post, I explained how I write tests with Rest Assured. See you in my next article, till then happy testing!

References

  1. http://rest-assured.io/
  2. http://makeseleniumeasy.com/2019/12/10/rest-assured-tutorial-14-interface-requestspecification-how-the-request-will-look-like/
  3. https://projectlombok.org/features/Data
  4. https://testng.org/doc/documentation-main.html

--

--