In this post, we will discuss how to Configure REST Assured with Eclipse in simple ways. In order to start our first script with REST Assured we should have our local environment ready.
Things we need in order to start with REST Assured
1- System (Laptop/Desktop), any Browser and Internet Connection (That you already have) 😉
2- Java- Any version of Java will work, In this series I will be using Java 8
3- Code Editor (Eclipse)- You can use any editor since I am good with Eclipse so I will be using the same for complete series
4- Maven – If you are using latest version of Maven then by default Maven plugin comes with eclipse.
5- Testing Framework- To validate result in Rest Assured we need some Testing framework Like TestNG or JUnit. I will be using TestNG for the same.
6- Your time, effort and patience 🙂
As of now these tools/ Libraries are enough to start with our REST Assured and once we will move to advance level we will use different tools as well like Jenkins, Cucumber, Git and Github and so on.
I already have a detailed video on the same.
Configure REST Assured with Eclipse
Step 1- Create a simple Maven project ‘RestAssuredProject’ in eclipse. The project structure will be as below:
Step 2: Find the latest stable version of ‘rest-assured’ Dependency
Navigate to http://rest-assured.io/
Navigate to footer section and Click on Getting Started Link and Copy the below dependency
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>3.1.1</version>
<scope>test</scope>
</dependency>
You can also use 3.1.0 new version as of now.
Step 3: Add the above Dependency in pom.xml of the project
After adding rest-assured dependency in pom.xml, save the file. The Maven Dependencies section will be added automatically. You can find the jars as shown below in your project.
Note: If the jar file is not added in the Maven dependencies section after saving, then clean and build the project.
Step 4: Create a new package and a new class
In src/test/java, create a package ‘getRequest’ and a java file ‘GetData’ as below.
Step 5: Add TestNG library to the project
Right click on Project -> Build Path -> Add Libraries -> TestNG
Step 6: Write the code as below:
We are using https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22Â
In the above class ’GetData’, we have initialized the variable api with a Sample API. You can also use any other API. There are 2 methods in this class:
- testResponseCode: Get the status code of the API and verify that the response is success(200) using Assert.
- testBody: Get the response time of the API and print it.
Rest Assured class provides the ‘get’ method for makng GET request to the API. The ‘getStatusCode’ method returns the response and the ‘getTime’ method returns the response time.
Note: We have imported io.restassured.RestAssured class and made it static so that we can use its methods(such as get method) directly without calling RestAssured class.
Step 7: Execute the test script and Verify the result
The following result will be generated after executing the above code using TestNG. The code and the Response will be printed in the console.
learner says
I followed this exactly but getting
[RemoteTestNG] detected TestNG version 7.4.0
java.lang.NoClassDefFoundError: Could not initialize class org.codehaus.groovy.vmplugin.v7.Java7
and
FAILED: testbody
java.lang.NoClassDefFoundError: Could not initialize class io.restassured.RestAssured
FAILED: testResponseCode
java.lang.NoClassDefFoundError: Could not initialize class org.codehaus.groovy.reflection.ReflectionCache
at org.codehaus.groovy.runtime.dgmimpl.NumberNumberMetaMethod.(NumberNumberMetaMethod.java:33)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
Mukesh Otwani says
Hi Mate,
Please try using different version of RestAssured. Which version you are using?
Balamurugan V says
Thanks Mukesh..
Clear artifact and demo video.. This is very useful..
sony says
I have added maven dependency in the project for rest assured jar but its jar is there but it is not identifying rest assured methods
Mukesh Otwani says
Hi Sony,
Do maven update for your project and try again
Manju says
to test the Microservice REST API, is there is any additional thing i need to do in the Maven project?
Mukesh Otwani says
Hi Manju,
If you are done with required configurations then you can go ahead with Microservices Rest API testing.
christina says
can we see the response data, for the API which we test using this??
Mukesh Otwani says
Hi Christina,
Yes, you will get whole response data in String format using asString() method
Shivaraj says
Dear Mukesh I am asserting status code through poi library means fetching expected status code through Excel but I am getting below error “reference to assertequals ambiguous” if I run through maven but it is working when I run through testing please guide me
Mukesh Otwani says
Hi Shivraj,
Using Apache POI, if you read any numerical data from excel then it always returns double type. Please refer this link for better understanding
Masthan says
hi Mukesh,
While try to execute the code, it is giving following mesage
java.net.ConnectException: Connection timed out: connect
i unable to find out why i am getting this exception
Mukesh Otwani says
Hi Masthan,
Are you running your script in your organizational network, if so then there is a network restriction.
RAAJ says
Hi mukesh,
I am testing SOAP services. How Can I validate XMl using REST assured ?
Mukesh Otwani says
Hi Raaz,
Rest Assured can only be used with Rest APIs which deals with JSON data not xml
Ruchi says
Hi Mukesh,
Can we configure eclipse with POSTMAN for api automation? If yes then can you pls provide video.
Thanks
Mukesh Otwani says
Hi Ruchi,
Are you using postman API for API testing or just Postman client tool. Could you please elaborate your requirement.
Milind says
Thanks Mukesh Sir
Milind says
Hello Mukesh Sir,
Please post below videos in detail with example:
1.Data providers and its use in selenium
2.Collection and its use in selenium
The above topics are the prime focus of interviewer.
It will really helpful to our emerging automation testers.
Thanks in advance.
Mukesh Otwani says
Hi Milind,
I have already covered them Data Provider http://learn-automation.com/data-driven-framework-in-selenium-webdriver/
Collection topics- List https://www.youtube.com/watch?v=OFjYA0apYGY you will also find for Set, Map
Thanks
Mukesh Otwani
shridhar says
is selenium manadatory for api testing and api test framwork design ?
Mukesh Otwani says
Hi Sridhar,
If you are looking for Rest based API testing then Selenium is not at all required. And for your information, Selenium itself is an API which allow us to interact Web UI through code.
Dhanraj K says
Hi Mukesh,
Your teaching is mind blowing. Keep rocking.
When can we expect sample code for PUT, PUSH and DELETE?
Mukesh Otwani says
Hi Dhanraj,
I have video for this please have a look https://www.youtube.com/watch?v=7RUzarUREpo&t=1704s
Sanu says
its really Fabulous Mukesh bhai..i will start soon Automating Postman API
Mukesh Otwani says
Hi Sanu,
Keep learning…:)
Anurag Bhatia says
Dear Sir,
You have given the methods for “get” request, thanks for that.
Can you please share the sample code for “PUT”, “PUSH”, “DELETE” & oblige ?
Thanks
Mukesh Otwani says
Hi Anurag,
I’ll upload remaining all soon…:)