By the end of this article, you will be able to set up and understand Rest API, Eclipse with TestNG and create Rest Assured Framework from scratch. Also including GIT Repository integration for Continous Integration.
Application Programming Interface (API)
In basic terms, APIs just allow applications to communicate with one another.
When people speak of “an API”, they sometimes generalize and actually mean “a publicly available web-based API that returns data, likely in JSON or XML”. The API is not the database or even the server, it is the code that governs the access point(s) for the server.
Web services are purpose-built web servers that support the needs of a site or any other application. Client programs use application programming interfaces (APIs) to communicate with web services. Generally speaking, an API exposes a set of data and functions to facilitate interactions between computer programs and allow them to exchange information. Web API is the face of a web service, directly listening and responding to client requests.
The REST architectural style is commonly applied to the design of APIs for modern web services. A Web API conforming to the REST architectural style is a REST API. Having a REST API makes a web service “RESTful.” A REST API consists of an assembly of interlinked resources. This set of resources is known as the REST API’s resource model. Well-designed REST APIs can attract client developers to use web services. In today’s open market where rival web services are competing for attention, and aesthetically pleasing REST API design is a must-have feature.
I recommend this youtube video to understand the basics of API and REST API: https://www.youtube.com/watch?v=7YcW25PHnAA
Install the latest version of the eclipse IDE from https://www.eclipse.org/, as the latest versions come with integrated Maven and GIT.
For detailed steps on eclipse installation, go to https://www.eclipse.org/downloads/packages/installer
Once Eclipse is installed,
On the menu bar, click Help.
Choose the "Install New Software…" option.
In the Eclipse Install dialog box
Enter "http://dl.bintray.com/testng-team/testng-eclipse-release/" in the Work with box
Click the Add button.
In Add repository dialog
Enter "TestNG" in Name Text Field
Click on Add button.
It will search for the entered software.
Select the "TestNG" checkbox
Click on the "Next" button.
It will give you a review of Items to be installed. Click on Next.
Select the radio button "I accept the terms of the license agreement"
Click on Finish.
It will take time depending on your Internet speed.
If you encounter a Security warning, just click "Install Anyway".
Wait for the installation to finish. When Eclipse prompts you for a restart, click "Restart now."
After the restart, verify if TestNG was successfully installed. Click Window > Show View > Other.Then open the Java directory and see if TestNG is included.
Once we are done with eclipse and TestNG Setup, we can start creating our Rest Assured Framework.
Rest-Assured is an open-source Java Domain-Specific Language (DSL).
Using Rest Assured we can eliminate large amounts of code to test complex API response and output.
It supports both XML and JSON format.
Rest-Assured supports many requests like
The precondition for Rest Assured Framework:
Once you have added this dependency you will be able to see Maven Dependencies added into your project. It will add all the related JARs to your project, but be sure to check these JARs as they are the most important ones:
httpclient: It is the base JAR which Rest-Assured internally uses
hamcrest-core: It will be used to see the patterns in the code and put assertions into the code.
json-path: If we get the data in JSON format, this JAR helps in formatting the data.
xml-path: If we get the data in XML format, this JAR helps in formatting the data.
Once you are done with dependencies, its time to hit the service URL.
I am going to use Postman as a manual tool which you can get from Chrome web store (go to Chrome Web Store and search for Postman, from there you can download the lite version of Postman) or full-fledged Postman application from their official Website https://www.postman.com/
For Rest Services Endpoint URL, you can go to https://openweathermap.org/api and browse many API service URL’s which is free. There are few paid ones too, you can check out on their website.
Open Postman and hit Send to below GET URL:
Once you hit the Send button, Response Status should be 200OK and you will receive the weather information of London in JSON Format.
Now that you have a perfectly working GET request, let’s automate the service using the Rest-Assured framework in Eclipse.
Congrats! You have created a Rest Assured framework from scratch and automated a GET service.
Now let’s create a GIT Repository and include our project into that. It is the most commonly used version control system. Git tracks the changes you make to files, so you have a record of what has been done, and you can revert to specific versions should you ever need to. Git also makes collaboration easier, allowing changes by multiple people to all be merged into one source
Creating a GIT Repository:
Go to https://github.com/ and create an account
Once the account is created GIT will ask you to create a New Repository (Start a Project).
Create a repository by giving it a Name, Description, and Type of Repository you want (Public or Private) and hit ‘Create Repository’.
Once you have selected GIT Repositories, enter your GIT Repository URL. Other fields will automatically populate, enter your git username and password in the Authentication section, and click on Next.
On Branch Selection window click on Next.
On the next window (Local Destination) select your local directory where you want to store your Git Project and click on Finish.
I have kept this repository public, you can copy my project from here: