Create Simple REST 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.

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.


Rest Testing Framework







We will be having three classes for the framework (in package com.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.

The first method (get in this case) executes the REST request returning a RestValidator object which can be used to perform the validations. Every method in RestValidator like expectCode, expectHeader, expectInBody etc returns the same RestValidator object so that the validations can be done in chained manner.


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.


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.

SEE ALSO:   Points to Consider During Framework Migration


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.

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

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

SEE ALSO:   Selenium WebDriver Framework: WebDriver Core Module

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.


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.

SEE ALSO:   How to Create Selenium WebDriver Automation Framework

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.

Further Reading:

What do you think of this simple REST testing framework. Can you think of a way to improve it? Let us know in the comments below.

You may also like...

19 Responses

  1. Akshay Awi says:

    I suggest use of Jersey client in place of HTTP will be better as there you will get more flexibility to form the URL or query parameters


  2. Chris Bates says:

    Hi, im wanting to give this project a try, i cant find a .pom file in the github link? is this not required?


  3. amit says:

    Hi amal,

    Nice work …
    I am not able to import this github project as a java prj in eclipse.
    Is it possible for u to convert it in maven or standalone java project.
    Also, how we start json-server to handle rest service requests kindly post those steps also.

    • amal bose says:

      Amit, I missed adding the .project and .classpath file to the repository. Thanks for pointing it out. I have added the files in the repo. You can download the project and Import into Eclipse as an existing project.

    • amal bose says:

      Here are the steps to start json-server.
      1. Install using the command
      npm install -g json-server

      2. Create a db.json file containing some data. Here is a sample one.
      “posts”: [
      “id”: 1,
      “title”: “rest testing framework”,
      “author”: “axatrikx”
      “id”: 2,
      “title”: “webdriver framework”,
      “author”: “axatrikx”
      “title”: “new test”,
      “author”: “axatrikx”,
      “id”: 3
      “title”: “new test”,
      “author”: “axatrikx”,
      “id”: 4
      “comments”: [
      “id”: 1,
      “body”: “some comment”,
      “postId”: 1
      “profile”: {
      “name”: “axatrikx”

      3. Start the server using below command
      json-server –watch db.json

      4. Navigate to url based on the data provided in db.json file.
      eg. http://localhost:3000/posts/1

      You can use GET, PUT etc requests to get and insert/update data to the db, or do it by directly editing the json file.

      Check their documentation to know more.

  4. sunil says:

    Please some one guide me to learn automation framework.. what is pom file what is xml etc… I have no stuff in automation.. very much interested to work on it.. but donno how to start it and from where

    • amit says:

      take training on selenium,appium,rest api automation
      if u have time you can self learn also from blogs….

  5. amit says:

    Hi Amal,

    Not able to delete using below code … what could be corrections

    public void testDELETEMethod() {
    * Delete operation for deleting resources.
    .expectHeader(“Content-Type”, “application/json; charset=utf-8”);

    HTTP returns 404 error
    get request shows resources are there at url

    “id”: 1,
    “body”: “some comment”,
    “postId”: 1

  6. Murali says:

    Hi ,
    can we test private Rest api using Oauth token. if yes, how do we implement in this code . if you have any sample , can you please share it .

    Thanks in advance.

  7. KIrtiman singh says:

    how can i use above code in MObile app automation ..
    For EX:
    Need to validate response code for Forgot password field??? Please help..

  8. Abhishek says:

    Hi Amal,

    In case of user authentication during a POST request,how to handle that. I mean to say what steps would be required.For example(auth token). I am in need to implement a validation of JSON response from back-end with the values populated in UI after doing certain search.


  9. Amal Bose says:

    that depends on how your authentication is implemented.. if u hav to provide the username and password, you can provide it as header. Same if you use an auth token from any where else.

  1. July 13, 2016

    […] Author: Amal Bose  Reference:  […]

Leave a Reply

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