Simple REST API Testing Framework


Even though there are several REST API Testing Frameworks available in the market today, they may not always suite your application, or may need modifications based on your requirements. This may call for the creation of your own rest testing framework.
We will be creating a simple Rest Testing Framework in Java and JUnit that could be used for any testing scenarios.

2318.WithAPIArchitecture

Rest Testing Framework Overview

The framework should be able to execute the basic REST operations (GET, POST, PUT, PATCH, DELETE) and perform the validations on the code, message, headers and body of the response.

The completed code can be accessed from my GitHub account from where you can collect and make modifications based on your requirements.

Design

Rest Testing Framework

 

 

 

 

 

 

We will be having three classes for the framework (in packagecom.axatrikx.controller)

RestExecutor : Performs the HTTP operations using Apache Http Client
RestResponse : A javabean class to hold our response values (code, message, headers, body)
RestValidator : Validates the response with the expected values

The package com.axatrikx.test holds the test scripts.

Note: I will be using ‘json-server‘ to fake the REST API. Its a real handy tool to rapidly setup fake REST API for testing your framework.

We are planning to have the framework implementation in the way shown below.

RestResponse

Its a JavaBean class holding the values from the response for each request. Four variables are provided each having its own getters and setters. See the class here.

RestValidator

This class performs the validation of the response with the expected values. It takes a RestResponse object in its constructor.
It has a method for each type of validations performed and all the methods returns the same object reference which enables chaining possible. The assertions are done using Junit assertions. See the class here.
The code is self explanatory and I’ve added a printBody() method to print the response body during script creation or debugging. This method also returns the Validator object and therefore can be used in the chain at any point.

RestExecutor

This class performs the HTTP operations using Apache HttpClient library and would be having a method for each of the operations. We will look into two methods in detail an you can check the code for the rest.

Constructor
The constructor is a simple one which takes in the url and initilizes the HttpClient object.

The GET method would have the path as one parameter and a HashMap as the second parameter for the headers provided as key value pairs. The method would be returning a RestValidator object containing the values corresponding to the response values of the GET request.

The HttpGet method is initialized with the combined URL, headers are set and request is executed to obtain the response. The response is then processed into our RestResponse object taking in our required values. This response object is used to initialize a RestValidator object corresponding to this GET request which is returned to the user to perform validations.

Note that I have added another GET method without the header parameter. This method just calls the above method with null value for header

POST Method
Post Method is similar to the GET method above, the only difference being the presence of xml body.

A simpler POST method without headers is also provided.

For the remaining methods, check out the complete class.

Now that the core of our framework is done, lets see how to write the test scripts.

TestScript

Since we are using jUnit as the unit testing framework, we can initialize the RestExecutor object in the @BeforeClass method and use the object in the @Test methods.

The executor.get(“/posts”) returns the RestValidator object having the RestResponse corresponding to the request. We can use the various validation methods on the object to perform the validation. As you might have noticed in RestValidator class, the assertions are using junit with the error messages preset. The remaining validations will be skipped if any one of the validations fail. Again, this can be modified according to your needs.

The same method is followed in the post request for the second @Test method. I haven’t added a proper reporting mechanism for this framework, but you can use any reporting libraries for JUnit or create one on your own.

See the completed project in github
I’d suggest to make this framework as Generic so that this can be used project independent. You can refer my article Most commonly used methods

Suggested: Basic concepts of APIAPI Testing using Selenium WebDriver And Java

Author: Amal Bose  Reference: Axatrikx.com 

Advertisements

21 thoughts on “Simple REST API Testing Framework

  1. Ajay July 13, 2016 / 10:30 pm

    Thanks for sharing Charan

    Liked by 1 person

  2. MD July 19, 2016 / 4:42 am

    Hello Charan,

    Thanks for the article. I am newbie in API testing and programming. I downloaded your source code but no idea how can I run this.

    Can you please throw some light on that. Also how I can use json-server?

    Thanks,

    Like

    • Hari Charan July 20, 2016 / 1:28 pm

      You can integrate the API’s you need to test. For example, if you send a post request, what response should be shown! Just a hint though

      Like

  3. Rest API Test - Raj July 19, 2016 / 5:17 pm

    Please share your email. Need some inputs on Automating the Rest API Tests.

    mail to blrtrends@gmail.com

    Like

  4. Hima Bindu July 30, 2016 / 1:43 am

    Thanks Charan for posting such an useful article. Can you always please provide some similar framework where we parameterize JUnit tests from Excel and run the tests please

    Like

  5. pepe August 1, 2016 / 12:24 pm

    A good, framework for testing with api, i have adapted all your framework for a usage with cucumber tests…

    However i have to investigate how i can use the method post, patch, delele and getentities, the method get was very easy by your example…

    I have to check the other methods.

    Regards,

    Like

  6. Anonymous November 3, 2016 / 3:42 pm

    Great article!!!!

    Like

  7. Anonymous December 7, 2016 / 7:09 pm

    Nice article!!! I was exploring on API testing using selenium and your article came in handy. Can this work on TestNG? do we need to change anything to make it work on TestNG

    Like

  8. yeri December 7, 2016 / 7:11 pm

    Hi Charan, Nice article. I was looking for API testing using selenium and this came in handy. What change to do to work on TestNg?

    Like

    • Hari Charan December 7, 2016 / 8:26 pm

      No, this can be integrated with any other frameworks. You need to use OOPS if you’re tying with functional part

      Like

  9. Anonymous December 9, 2016 / 6:28 pm

    How to give authentication in header here?

    Like

  10. Anonymous December 10, 2016 / 4:28 pm

    The api works fine when given as url on a browser but I get SSL exception when tried to do this through httpurlclient/httpget/httpclient using selenium.
    Is there some setting that i need to take care?

    Like

  11. Nikhil March 16, 2017 / 2:19 pm

    How do i post Multi Array Json? in below mentioned format.

    (“/posts”, “{\”token\”:”+passtoken+” , \”id\”:\”49602\”}”, “application/json”)

    Like

  12. Srini April 16, 2017 / 6:04 pm

    Hi,can you please provide me input files ,which one are you using json or xml

    Like

    • Hari Charan April 17, 2017 / 12:25 am

      You can try with your API, URL mentioned in the article was loopback address. This program supports Json

      Like

  13. Anonymous May 5, 2017 / 2:25 pm

    Hi Charan,

    Can you please let me know how to validate the json response using JasonObject class.

    Like

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s