As we already discussed Rest Assured at a high level on this page as it is a Java-based library that helps to write powerful and robust code to test RESTful APIs. In this rest assured example, I’m going to show you how to combine the individual pieces to perform REST API testing using REST Assured like Dependency configuration, writing up and understanding the REST-assured syntax, and finally will see how to validate the Response.

 

Let’s begin:

Elaborative and yet easy representation of REST API testing using a REST Assured example...!!! Click To Tweet

Dependency Configuration:

I’ve already explained this in my previous post, please go check this link for thorough understanding. But, let me explain you to how to do this in brief here.

Simply add the below-mentioned dependency to your maven project’s pom.xml.

Note: you can choose the ‘version’ of your choice.

In order to run the RESTassured tests, we need to integrate it with any existing unit testing frameworks like TestNG or JUnit. Here in this tutorial, we are going to use TestNG. And, there are two pieces of doing that.

First is to add below TestNG Maven dependency into your project POM:

Note: you can choose the version of your choice.

And Second, install the TestNG plugin in your eclipse. Check this link to see how to do that.

Now, we are all set to go deep into the RESTassured.

 

Let’s go through the syntax :

In this rest assured example, we will test the ‘Dummy Sample Rest API’ which is available here. This page contains Fake Online REST API for the testing purpose which is performing various CRUD operations.

Let’s take an example of one of the API endpoints available at the above-mentioned website which is ‘/employee/{id}’ where the full-service URL with the endpoint is ‘http://dummy.restapiexample.com/api/v1/employee/{id}‘. It’s a simple GET request and we are trying to access the details about the employee whose id is 16914 in this below example.

 

 

Above mentioned style of code (given/when) comes from ‘Behavior Driven Development (BDD)’. It’s very easy to understand and easy to write. (Given/When/Then) BDD flow helps you to do everything in one line.

Let’s look at some details about the method’s being used above from RESTAssured API:

  1. given()

           package: 

                RequestSpecification io.restassured.RestAssured.given()

          Description:

                Start building the request part of the test io.restassured.specification. E.g.

will send a POST request to “/greetXML” with request parameters firstName=John and lastName=Doe and expect that the response body containing JSON or XML firstName equal to John.

          Returns:
                A request specification.

 

      2. when():

            package:

                   RequestSpecification io.restassured.specification.RequestSpecification.when()

           Description:

               Syntactic sugar, e.g.

               is that same as:

          Returns:
     the request specification

 

      3. get():

            package:

                   Response io.restassured.specification.RequestSenderOptions.get()

           Description:

               Perform a GET request to the statically configured path (by default http://localhost:8080).

          Returns:
     The response of the GET request.

 

Now, with the Response object (response) as per the above example, we can collect various sorts of data like status code, response body content, etc . and also perform validation using TestNG assert or with other APIs like Hamcrest Matchers which I’ll show you in the following tutorial posts. But, here I am validating the status code using TestNG assertEquals method with the blow line of code.

 

Response Validation:

As per the above line of code, we are checking whether the intended status code value which is 200 in our case is matching with the retrieved one from the response object using the getStatusCode() method.

 Elaborative and yet easy representation of REST API testing using RESTAssured...!!! Click To Tweet

 Now, Let’s execute the above Test class (GetRequest) in eclipse and verify the output:

a. Check for the Response i.e {“id”:”16914″,”employee_name”:”test”,”employee_salary”:”123″,”employee_age”:”23″,”profile_image”:””}

b. Check for the Status Code i.e ‘200’ as intended.

c. Check if the ’employee_salary’ string present in the response i.e ‘true’ as intended.

d. Lastly, we performed assertEquals verification for Status Code, which is successfully passed, hence no failure in the eclipse output.

Eclipse Console Output:

 

Elaborative and yet easy representation of REST API testing using a REST Assured example...!!! Click To Tweet

 

That’s it, it’s that simple to perform REST API testing using REST Assured API: ?

Other References: