The More You Learn The More You Play…!

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 accountfrom where you can collect and make modifications based on your requirements.

Design

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

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

public RestValidator get(String path) {
return get(path, null);
}

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

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

hey nice article but can you please tell how i can use ‘json-server’ . i have download the package for the same link you given above but not get exact way and pleade give complete details for ‘json-server’ that to add this in the same project(your project which is downloaded from git).