DEV Community

Cover image for Rest-Assured Framework from scratch including GIT Repository integration for Continous Integration
Sahil Tiwari
Sahil Tiwari

Posted on

Rest-Assured Framework from scratch including GIT Repository integration for Continous Integration

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.

What is API?

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.

REST APIs:

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.

Alt Text

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

Setting up Eclipse:

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

Setting up TestNG:

Once Eclipse is installed,
Launch Eclipse.
On the menu bar, click Help.
Choose the "Install New Software…" option.

Alt Text

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.
Alt Text
In Add repository dialog
Enter "TestNG" in Name Text Field
Click on Add button.
Alt Text
It will search for the entered software.
Select the "TestNG" checkbox
Click on the "Next" button.
Alt Text
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".
Alt Text
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.
Alt Text
Once we are done with eclipse and TestNG Setup, we can start creating our Rest Assured Framework.

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

  • GET
  • POST
  • PUT
  • DELETE
  • PATCH
  • HEAD

The precondition for Rest Assured Framework:

  • Java
  • Maven
  • TestNG

Now head to eclipse and start creating a project.
Go to File>New>Other
Alt Text

Select Maven>Maven Project
Alt Text

Select “Create a simple project” and click on next
Alt Text

Now give logical names to “Group Id” and “Artifact Id” and click on Finish
Alt Text

Congrats! You have created a Maven project successfully.
Alt Text

Now let’s add the rest-assured dependency to POM.xml. In your project open POM.xml and add below dependency:
Alt Text

Now your POM.xml should look like this:
Alt Text

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.

Alt Text

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:

http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=439d4b804bc8187953eb36d2a8c26a02

Alt Text

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.

Go to your project and create a package under src/test/java. Right-click on src/test/java folder and create a package named “serviceRequest”. Under that create a Java class called “getData”.
Alt Text

Now write the code in Rest-Assured Project to get the same data we got in Postman and assert the Status:
Alt Text

Your class should look like this:
Alt Text

Right-click anywhere on Java class and Run as> TestNG Test
Alt Text

You should be able to see same response in the console which you received in Postman. Moreover, we have performed an assertion on the Status Code.
Alt Text

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

GIT:

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’.
Alt Text

Your Repository will be created, copy your Git Repo URL:
Alt Text

Go to eclipse:
Click on the Top Left Search button also called “Access commands and other items” (Ctrl+3) and search for Git Repositories.
Alt Text

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.
Alt Text

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.

Your Git Repository will be added to the Eclipse-Git Repositories window.
Alt Text

Now, right-click on your project and go to Team> Share Project
Alt Text

Select your repository from Repository dropdown and click Finish
Alt Text

Commit and Push the project to the GIT repo:
Alt Text

Alt Text

Select all Unstaged Changes and drag it to Staged Changes and give Commit Message
Alt Text

Push Branch in Remote window will show you details like Source and Destination of your GIT Branch. Click Next
Alt Text

It will ask for Push Confirmation. Click Finish.
Alt Text

You have successfully pushed the local code to GIT. Go to your GIT Repository and refresh it. You should be able to see all local code into your repository.
Alt Text

I have kept this repository public, you can copy my project from here:

https://github.com/sahilTiwariQA/rest_AssuredFramework

References:
Rest API Design Rulebook - O’Reilly
https://www.eclipse.org/
https://www.guru99.com/
https://mvnrepository.com/
http://rest-assured.io/
https://openweathermap.org/api

Top comments (0)