Building End-to-End API Testing Framework: Part 7.1
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.
Make sure you provide the same API name which we have defined in the enum class, which we discussed in the previous part.
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.
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.
Now, check-in the console output
Now, we need to copy this code and paste it into the step definition file.
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.
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.
Now, let’s build our GET request using the utility we just created.
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
And, the equivalent code we will be using is
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.
Now, when we hit the get API, the response variable will be loaded with the new response.
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.
Make sure you write the proper key based on the GET API JSON response.
You can also verify it in the Postman.
Now, we need to compare the extracted book name with the expected name.
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.
Now, let’s run the test cases with the Test Runner file
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.
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.
Let’s run the tests and see if the brackets are removed in the console window or not.
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.
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 🍻