REST Assured

How to Configure REST Assured with Eclipse

Configure REST Assured with Eclipse

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.

Configure REST Assured with Eclipse

 

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.

Dependency

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 

Code

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:

  1. testResponseCode: Get the status code of the API and verify that the response is success(200) using Assert.
  2. 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.

output
Hope you liked the article and if yes then please share with your friends.
author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

28 thoughts on “How to Configure REST Assured with Eclipse

  1. 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)

    1. Hi Mate,

      Please try using different version of RestAssured. Which version you are using?

  2. Balamurugan V says:

    Thanks Mukesh..

    Clear artifact and demo video.. This is very useful..

  3. 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

    1. Hi Sony,

      Do maven update for your project and try again

  4. Manju says:

    to test the Microservice REST API, is there is any additional thing i need to do in the Maven project?

    1. Hi Manju,

      If you are done with required configurations then you can go ahead with Microservices Rest API testing.

  5. christina says:

    can we see the response data, for the API which we test using this??

    1. Hi Christina,

      Yes, you will get whole response data in String format using asString() method

  6. 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

    1. 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

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

    1. Hi Masthan,

      Are you running your script in your organizational network, if so then there is a network restriction.

  8. RAAJ says:

    Hi mukesh,

    I am testing SOAP services. How Can I validate XMl using REST assured ?

    1. Hi Raaz,

      Rest Assured can only be used with Rest APIs which deals with JSON data not xml

  9. Ruchi says:

    Hi Mukesh,

    Can we configure eclipse with POSTMAN for api automation? If yes then can you pls provide video.

    Thanks

    1. Hi Ruchi,

      Are you using postman API for API testing or just Postman client tool. Could you please elaborate your requirement.

  10. Milind says:

    Thanks Mukesh Sir

  11. 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.

    1. Hi Milind,

      I have already covered them Data Provider https://vistasadprojects.com/mukeshotwani-blogs-v3/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

  12. shridhar says:

    is selenium manadatory for api testing and api test framwork design ?

    1. 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.

  13. Dhanraj K says:

    Hi Mukesh,
    Your teaching is mind blowing. Keep rocking.
    When can we expect sample code for PUT, PUSH and DELETE?

  14. Sanu says:

    its really Fabulous Mukesh bhai..i will start soon Automating Postman API

    1. Hi Sanu,

      Keep learning…:)

  15. 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

    1. Hi Anurag,

      I’ll upload remaining all soon…:)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.