In this article we will talk about, how we can effectively use JSON-server to shift left our API Automation testing.

JSON-Server comes handy mainly in the below 3 situvations

  • When Developer is building the API and QA is waiting for the development activity to complete to start testing and automating the API
  • When an external system is exposing an API which is required for an end to end scenario testing and the team is waiting for it to get ready to start testing and automating the API
  • When a particular API is not available in lower environment (Development), so the developer can only test the APIs on higher environments.

So how can we leverage JSON-server to tackle the above mentioned problem scenarios ? We can use JSON-server as a Mock API framework and connect it to the application in place of the under development API or API which doesn’t exists in that particular environment.

Now let’s see how this can be done.

Install and Setup the JSON-Server

We can easily install the JSON-Sever by using npm. Open the command prompt and enter the command “npm install -g json-server” . This will install the JSON-Server globally in your machine.

Create a db.json file

Now lets create a db.json file, which will be acting as the API source. Create any json file with any name. For this article I am creating a db.json file which contains the below content. Once the file is created, save the file in any prefered path.

Start the JSON-Server

Once the db.json file is created, navigate to the folder where the file is saved. From the folder path, open a command widow and type the command json-server –watch db.json

Now we can see that the server is running at http://localhost:3000. Open the address in the browser and you can see the API details and definitions.

With this we have successfully setup the JSON-Server. Now let’s see how can we start performing API testing using this Mock server.

Assuming that the db.json file mimics AUT API responses, open the POSTMAN (or any other API tool) and get the necessary GET requests to start performing the tests.

GET and POST requests to JSON-Sever using POSTMAN

GET request – http://localhost:3000/LookUp

This will display all the LookUp values.

Use can use the GET request http://localhost:3000/LookUp/1 to get the particular value set.

POST request – http://localhost:3000/Lookup with a request body as below will create a new record in the db.json file.

 {
"Lookupid": "B0000",
  "name": "zeroth pvt ltd",
  "AUM": 9547
}

Note: For POST/PUT/DELETE requests a header content-type = application/json should be added.

I hope you all liked the article. Please let me know your comments below.

References: https://github.com/typicode/json-server