Rest Assured E2E Testing - Part 2
Tags: restapi, automationtesting, restassured
Published: 2021-09-05 12:56:14

#How to create a Fake REST API with JSON-Server
We currently do not have any real business exposed APIs with us for testing, so we will be creating dummy APIs (which will be provided by the developers in the real time scenario) using NodeJS. The npm package in Node JS will help us even more by installing JSON Server, which will allow us to create dummy APIs for practise.
To install Json server, first install nodejs and npm (npm is installed automatically when we install nodejs).

#Installation and Configuration of NodeJS: 
1. Go to google.com and search “download Node JS
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l08kg5tyemsyswwu6j59.png)

2. Download Node.js from the link https://nodejs.org/en/download/

3. Select the installer based on your system, I am downloading 
windows installer 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/584nqonofs7usqdsumjr.png)

4. After successfully downloading, install by clicking next -> next -> next till finish

5. The nodejs setup can be found in the nodejs folder, which is usually created by default at C:\Program Files\nodejs. Start the setup. Copy the path C:\Program Files\nodejs from the nodejs folder.
 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a35tghezl4d43fh9phvr.png)

6.  Now set up the path variables as shown below
 In the search bar, enter sys and select Edit the system environment variables.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bemkkw2j6fbw4qd9qlp4.png)

7. Click on Advanced tab and Environment variables
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ros47dl0dfcluqte3q0.png)
 

8. Under system Variables, click on new and add as below
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxz5h3ke0yrsjnvnustu.png)

9. Select Path Variable under the System variables and click on Edit Button
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qxpbptcgv9jcf0crigt0.png)
 
10. Click on New and Paste the path that was copied earlier in step 5 C:\Program Files\nodejs and click Ok…Ok and Ok.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/408xq5u846njbvjpf6g4.png)

11. Verify the installation in command prompt by entering node -v
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e5yegef4x5uxaoeo7ixo.png)

13. Please note that after installing nodejs, another file named npm (node package manager) is installed under the nodejs folder, and this npm facilitates the installation of various nodejs packages.
 verify npm installation by typing the following command: C:\>npm - -version

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pc3bl5547tm4mb2a3774.png)

##Installing Json Server 
JSON Server is available as a NPM package. The installation can be done by using the Node.js package manager:
####$ npm install -g json-server
By adding the -g option we make sure that the package is installed globally on your system.
   
##JSON File
Let's make a new JSON file called db.json. This file contains the data that the REST API should expose. CRUD entpoints are automatically created for objects contained in the JSON structure. Take a look at the sample db.json file below:
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/49u3eh4jcocgl9pmgtvz.png)

##Running The Server 
Let’s start JSON server by executing the following command:
###$ json-server --watch db.json
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/79fgrlagvwsavtxuq3zf.png)

As a parameter, we must supply the file containing our JSON structure (db.json). In addition, we're employing the — watch parameter. By using this parameter, we ensure that the server is started in watch mode, which means that it monitors for file changes and updates the exposed API as needed.

The json server is up and it has created four dumpy API’s for us as per the json data present in file:
 • http://localhost:3000/employees 
• http://localhost:3000/posts
 • http://localhost:3000/comments
 • http://localhost:3000/profile

Now we can open URL http://localhost:3000 in the browser and we’ll get to see below result.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/blleud7s8j2e8jogk2u1.png)

To terminate the server we have to type Ctrl+C command and then press Y as per confirmation message prompted below

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eqpc4h753w6y2ttp3zdn.png)