Saturday 30 January 2021

How to Create fake REST API using Json-server and use from Postman to perform API Testing?

 


 Check below link for interview Q&A  API TESTING:
 


Fake REST APIs created using JSON Server are essential for testing purposes. They offer a valuable means of independently testing frontend applications without relying on a fully developed or available backend server. 


By defining custom routes and responses in a JSON file, developers can simulate different server responses, including success, error, and edge cases, to ensure proper handling by the frontend. These fake APIs enable the reproduction of specific scenarios that may be difficult to replicate with a real backend, such as slow network connections or specific error conditions. 


Moreover, they facilitate rapid prototyping, allowing frontend developers to start building user interfaces and interact with simulated data without waiting for the complete backend functionality. Fake REST APIs also serve to test edge cases, providing a controlled environment for assessing how the application handles unusual scenarios and ensuring optimal performance. 


Ultimately, the use of fake REST APIs with JSON Server enhances testing efficiency, isolates the testing environment, and enables comprehensive evaluation of frontend applications.

JSON Server is a simple project that helps you to setup a REST API with CRUD operations very fast. So let's learn how we can create fake rest api using json-server, It is useful for building frontend apps and for testing integration components. 

Step-1:
Download node js, refer below link:
https://nodejs.org/en/download/

Open a command prompt (or PowerShell), and enter the following:

node –v

The system should display the Node.js version installed on your system. You can do the same for NPM:

npm –v

For above two commands you should get response as below :


C:\Users>node -v
v10.16.3

C:\Users>npm -v
6.9.0



Step:2

npm install -g json-server

***************************

 DevOps Interview Question Set

***************************

Step-3

Create a json file as db.json and open cmd in that path
Define the json format:
{
  "employees": [
    {
      "id": 1,
      "first_name": "sidharth",
      "last_name": "shukla",
      "email": "sidharth@automationreinvented.com"
    },
    {
      "id": 2,
      "first_name": "Steve",
      "last_name": "smith",
      "email": "steve@automationreinvented.com"
    },
    {
      "id": 3,
      "first_name": "virat",
      "last_name": "kohli",
      "email": "virat@automationreinvented.com"
    }
  ]
}

Step-04:

Run the below command from command prompt
json-server --watch db.json


Step-05:

By Default it will take localhost:3000, Now we can open URL http://localhost:3000/employees in the browser

*************************************************

Framework Design Tips & Interview Question-Click Here

********************************************

Step-06:
How to test in Postman?


Just open postman tool and ru below URI:


It's possible to extend URLs with further parameter. E.g. you can apply filtering by using URL parameters like you can see in the following:


http://localhost:3000/employees?last_name=shukla
http://localhost:3000/employees?first_name=sidharth
http://localhost:3000/employees/1

Validate the status code and response body.

Automation Testing/SDET Framework Design Link

Java Related Interview Question Set LINK

GIT Interview Question Set LINK

Step-07:

The following HTTP endpoints are created automatically by JSON server:


GET    /employees
GET    /employees/{id}
POST   /employees
PUT    /employees/{id}
PATCH  /employees/{id} 

So all the above methods can be tested both from Postman or Rest Assured.

NOTE: Learn (API-Microservice)Testing+(CoreJava+UI)-SDET with Self Paced Videos and one LIVE Doubt Session with the Author of this blog who is working as an SDET @ Amazon.

*******************************************************************
For any doubts or career guidance from me, reach out here : https://topmate.io/sidharth_shukla

********************************************************************

Do remember that knowing Linux is one of the most important aspect to work as an SDET.

Basic Linux Commands for Automation QA


****************************************


SELF PACED TRANING VIDEOS AVAILABLE with Doubt Session @4500/-(API+Postman+RestAssured+Framework+GIT+Jenkins+Project+DoubtSession(1-1)) and 6500/- (API+UI) Check Training Page for Course Content or reach out @whatsapp +91-9619094122




***********************************************************

************************************************* 

  • Explain testNG annotation in sequence with real time scenario?example of @After@Before in TestNG?selenium interview qus/testng

Click Here For Answer

*************************************************

SeleniumWebdriver Automation Testing Interview Questions:

API Testing Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Rest-API

 
Kubernetes Interview Question Set
https://automationreinvented.blogspot.com/search/label/Kubernetes

 
Docker Interview Question Set
https://automationreinvented.blogspot.com/2020/02/top-18-docker-commands-for-aytomation.html

 
Linux Interview question Set
https://automationreinvented.blogspot.com/search/label/Linux

 
Automation Testing/SDET Framework Design
https://automationreinvented.blogspot.com/search/label/FrameworkDesign


Java Related Interview Question Set

https://automationreinvented.blogspot.com/search/label/Java


GIT Interview Question Set:
https://automationreinvented.blogspot.com/search/label/GIT


Coding Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Coding%20Questions

 

No comments:

Post a Comment

All Time Popular Posts

Most Featured Post

API Status Codes with examples for QA-Testers

  🔺 LinkedIn: https://www.linkedin.com/in/sidharth-shukla-77b53145/ 🔺 Telegram Group:  https://t.me/+FTf_NPb--GQ2ODhl 🏮In API testing, it...