Building Cucumber API Testing Framework from scratch: Part 6
In this part, we will use the enum class to make the code more optimized.
For instance, we are passing the API endpoint details in our step definition file which is not good you should drive it from any other centralized file so that whichever test case using this add JSON should take it from that file, so tomorrow, if something changes in the resource, that we can simply change in one single place and all the test cases will be updated automatically.
what is the enum class in java?
A Java Enum is a special Java type used to define collections of constants. More precisely, a Java enum type is a special kind of Java class. An enum can contain constants, methods, etc.
Why do we need an enum class?
Let’s say there are 100 APIs for an eCommerce application that we need to drive from a file, now to call every API we have to create the same number of methods that can return the API string, and doing that we have to come up with a lot of coding which is definitely not recommended.
So, that’s where enum comes into the picture. In the enum class, you only require to define a single method that will drive all the API strings.
How to create an enum class?
Go to your resources package, right-click and create a new class called API Resources.
Now, provide the name in the new Java Class window and click on the Finish button.
Once the class is created, you need to replace the keyword class with enum.
Now, let’s declare the Add Book API which we are using in the Step Definition file.
Enum class will treat this as a method, and it will return the string which we have provided for the API
Similarly, you can declare multiple constants and separate the constants with a comma. In our case, we can add Get Book and Delete Book API.
Now, mouse-hover on the method and it will ask you to create a constructor for the method.
Click on create constructor ‘APIResource(String)’ and it will create the constructor for you with a String argument to match with the methods.
Now we need to make some changes in the feature file with the when step so that it can drive the API string by simply change the name under double-quotes.
Now, we need to call the Add Book API method in the Step Definition file but to do so we first require to create one method in the Enum class which will return the API string with the variable called resource and we need to assign the global variable to the local variable resource.
Now, we need to call the API string in the Step Definition file by creating the object of the Enum class.
Now, we can replace it with the endpoint/resource we are providing in the post-call right below this and we will also print it in the console output to verify which method is getting called from the Enum class.
Let’s run the test cases with the Test Runner class and see if it’s working fine.
From the console output, you can see it’s asking us to modify code in the Step Definition file. So, let’s replace our code with the one showing in the console.
You can also change the variable name as per your understanding as you can see I have given resource and method but you can give anything.
Now we will make the step definition class code generic for HTTP methods as well.
If you notice you have made the Step Definition class so generic that it can accept any HTTP method and resource, which will make this class more powerful than before.
To verify if the code we have developed is working fine or not, let’s call Delete Place API from the feature file and check in the log file.
Let’s check in the console since we are also printing the API Resources method and of course our test will fail because we have not provided the steps for the Delete method.
Also, we can check the log file regarding what’s really happening in the backend.
So, we are able to call the Delete Book API through the features file which means our code is working, and now let’s change it back to Add Book API to call the Add Book URL.
And, now let’s run our tests one more time to make sure everything is working as expected. Make sure you alter the ISBN and aisle information for the reason I have mentioned above.
Also, let’s check in the log file if the add request is getting called or not.
Alright, so that was all about enum class and how you can optimize your code through it.
Thanks for sticking around so far.
Here is the link to the GitHub repository to download the code.
If you are not sure what we are doing here, then go and check out my take on Cucumber, Maven, and get started with Building API Testing Framework from scratch.
Adios 🍻