Building End-to-End API Testing Framework: Part 7.1

Prem Singh Rathore
5 min readAug 3, 2021

--

In this part, we will add one more scenario using the GET method where we will call the method and check if the book name we have added in the POST method payload matches the name in the get call response.

Now to do so, we need to provide one similar line in plain English in the feature file, which will call the GET API string, and extract the name from the response body.

Feature File

Make sure you provide the same API name which we have defined in the enum class, which we discussed in the previous part.

Enum Class

Also, let’s comment out the second line in Examples since we are only concerned about the first payload and, it was part of the parameterization anyway.

Feature File

Note: In the feature file, you can comment out any line using the ‘#’ sign before any statement.

We need to get the equivalent code for the step definition class to tag to the line we just defined in the feature file.

For that, you need to run the tests once with the Test Runner class and check the console output for the equivalent code. Don’t forget to alter the ISBN and aisle information to prevent the 404 error.

Test Runner Class

Now, check-in the console output

Console Window

Now, we need to copy this code and paste it into the step definition file.

Step Definition Class

Before we start customizing the code, we need to get the Book ID which will pass in the form of a query parameter in the GET method.

To do that let’s defined one method in the Utils class which will get us the Book ID.

Utils Class

Make sure the return type of the method is String

Now, we have built a utility that can give us the value of any key, which we will provide as a parameter from any JSON response.

First, let’s replace this utility method with the one we are already using in the step definition file.

Step Definition Class

Now, let’s build our GET request using the utility we just created.

Step Definition Class

Now, to hit the GET API request using the “GET” HTTP method we can actually use the same code which we used for ADD BOOK API using the “POST” HTTP method

Feature File

And, the equivalent code we will be using is

Step Definition Class

So, we need to pass the resource, i.e., Get Book API to get the API string from the enum class and “GET” method for hitting the GET API request from the if-else condition.

Step Definition Class

Now, when we hit the get API, the response variable will be loaded with the new response.

Step Definition Class

So, our goal is to extract the book name from the new response.

Simply call the JSON Path utility method to get the Book Name.

Step Definition Class

Make sure you write the proper key based on the GET API JSON response.

You can also verify it in the Postman.

Get Request Window In Postman

Now, we need to compare the extracted book name with the expected name.

Step Definition Class

So, sit back and relax because the moment of checking whether our code is working as expected or not is here.

Make sure you change the payload information for clarity purposes.

Feature File

Now, let’s run the test cases with the Test Runner file

Console Window

Oops 😕

Don’t worry we have got the assertion error because our actual book name is coming from the JSON array list therefore, it’s failing to compare both the values.

Console Window

Now, to resolve this error we need to remove the square brackets by adding one simple line of code as follows:

String output = arraylist.toString().replaceAll(“(^\\[|\\]$)”, “”);

In our case, we only need to add from replaceAll and append it to the actual book name variable.

Step Definition Class

Let’s run the tests and see if the brackets are removed in the console window or not.

Console Window

Awesome, now let’s put back the assertion in the tests and run for the last time to make sure nothing is breaking and everything is working as expected.

JUnit window

Alright! So, we have successfully integrated the GET method scenario as we proposed.

Phew 😅

In the next part, we will integrate the DELETE method scenario and that would conclude the end-to-end testing framework.

And, Here is the link to the GitHub repository to download the code.

Cheers 🍻

--

--