Today every application is highly relying on API usage to support multiple features. That's why it's very important to test API's before production. Sometimes, in complex application hundreds of API call are made. In such scenarios API automation is must to make process faster and more reliable. Thankfully there are multiple automation tools available for API automation. In this blog we are going to set up REST Assured API framework step by step.
Table of content
REST Assured is Java library to test Restful Web services. It is used to invoke REST web services and responses. REST assured framework also provides the flexibility to write BDD-style scripts to enhance readability. Integrating rest assured into a ci/cd pipeline is easy. To create proper framework in JAVA we can use popular frameworks like Junit or TestNG. REST assured itself is build in Groovy language.
In this article we are going to set up REST assured framework from scratch.
1. Install Java or check if Java is installed on your machine. You can check version of your java by going to your command prompt.
2. Install IDE like Eclipse, IntelliJ. Here I'm going to use Eclipse which you can download from https://www.eclipse.org/downloads/
3. Install Build tool - Maven from here https://maven.apache.org/download.cgi. You can check if it is already installed. Eclipse already comes with Maven but if you want to run your project from command line then you need to install it.
4. Determine Java project framework. We'll be working with TestNG framework.
Creating Maven project in Eclipse
In Eclipse go to Project > New > Other > Select Maven project
Next select Create Simple project . Below window will appear. Here you provide Group Id and Artifact Id. Then click on Finish.
Your Maven project is created.
Creating your working space
You can crate new working space for your REST Assured projects. It will keep your work organized in your Eclipse and you don't get distracted with other projects.
1. In Eclipse, click on 3 dots on top left side bar near Project Explorer.
2. Go to Select Working set > Click On New > Create New working set > Give name to your working set.
3. Your New working set is now created. You can add your recently created Maven project in this working set.
Expand your project and go to POM.xml. Here we add dependancies that we will be using for our project.
3. Paste dependency in your POM.xml > Save your Project. Now you can see that Maven Dependencies are added to your project.
Similarly you can add all required dependencies to your project. In future if version of any dependency changes we can easily manage them in POM.xml. Or in future if we need to move our project to different system, we don't have to worry about moving jar files. All we have to maintain is POM.xml. This is the advantage of such a library management in Maven.
Install TestNG Plugin
We need to add TestNG plugin. The TestNG Eclipse plug-in allows you to run your TestNG tests from Eclipse and easily monitor their execution and their output.
Find latest release and copy that URL
3. Now go to your Eclipse project > Help > Install New Software > Add above URL in front of Work with. > Click on Add. Provide name and again click on Add.
4. You can now check that your TestNG plugin is installed. Select CheckBox in front of TestNG > Next > Next > Finish the installation.
5. After this new software installation, Eclipse will prompt to restart. You should click on "Restart Now". This will install all latest updates to your Eclipse.
6. You can check if TestNG is installed. Go to Eclipse > About Eclipse > Click on installation Details.
7. Search for TestNG and you'll see TestNG libraries installed.
Now our set up for RestAssured frame work is completed.
Create First Test
Open Eclipse > Go to your project > Go to src/test/java
Create new package > Create New class
Create a function and annotate it with @Test (TestNG)
Create a GET request. For testing purposes there are many open test API's are available online that can be used.
Import TestNG and RestAssured libraries to your code.
6. Right click on the tests.java > Run as TestNG test. You can see console output. We are getting status code 200. It means our GET request was successful. We also successfully got the response time which is in milliseconds.
7. You can also check TestNG Results. As you can see our testNG tests are passed.
We have successfully set up our REST Assured framework and ran our first basic test.
Why REST Assured ?
Sending Http request and receiving response is breeze with this framework. Without Rest Assured libraries writing Http request and response is very tedious task.
REST Assured is java library so continues delivery and integration is easier.
Uses Gherkin given, when, then format which makes tests extremely readable.
Verifying technical response data is just as straightforward as checking response body contents.
Data driven testing can be done in very efficient way.
REST Assured offers a wide range of other useful features that can create powerful automated tests for our RESTful APIs. We'll take deeper dive in next blogs!
Happy learning !