top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

API Servers are not ready for testing☹ No more waiting for them. HOW?? Let’s MOCK! Let’s Rock!! 😊

Introduction:

What’s Mock? It’s a copy of the real one. So can we make a copy of the API server? Yes, Mock Servers are the way we can simulate real API servers. Here in this blog, we will discuss the following topics about Mock Servers:


I) Why Mock?

II) What do Mock Servers mean?

III) When Mock Servers can be used?

IV) How to Mock API servers using POSTMAN with an example?



I) Why Mock?

Simple architecture for HTTP requests (e.g., GET, PUT, POST, DELETE) involves two steps:

1) The client sends a request to the Server

2) The server processes the request and sends an appropriate response to the client.


But developers need time, correct input, and output data as expected from business to build Actual Working API. In today’s fast-moving world, where Front end developers, Back-end Developers, and QA teams will work simultaneously on projects, testers like to start writing test scripts and testing the software simultaneously. So, testers don’t want to wait for developers to give them working API endpoints or even developers don’t want to wait for correct data to be provided to develop and test their APIs.


By now we all know that this is why we need to create “Mock Servers! Let’s dig more into it.

The diagram below further explains why we need to Mock Servers.



II) What do Mock Servers mean?

A mock server, in simple words, is to simulate API requests and the corresponding responses by creating a copy of a real server. we have to set up a request and add a response in the example based on API specification Document or Contract Testing Document.


A mock server will be generated and give us one URL link. We can use that link and check or test our response.


The response data can be Static or Dynamic.


3) When Mock Servers can be used?

Following are a few cases where Mock Servers can be used effectively:


· When API endpoints are not ready, fake servers will facilitate testing by sending requests to them.

· A mock API server is useful during development and testing when live data is either unavailable or unreliable.

· While designing an API, mock APIs work concurrently on the front and back end, as well as helps to get comments from developers.


4) How to Mock API servers using POSTMAN?

Postman enables us to create mock servers to assist with API development and testing. A mock server simulates the behavior of a real API server by accepting requests and returning responses. By adding a mock server to our collection and adding examples to our requests, we can simulate the behavior of a real API.

When we send a request to a mock server, Postman matches the request to a saved example in our collection. Postman then responds with the data we added to the example.


For a demonstration of creating mock servers, we can use https://reqres.in/.


For Example: To get a list of All users when the actual Server is ready


Request: https://reqres.in/api/users?page=2

Response code: 200

Response Body: As shown below





Let’s assume that developers are not ready with working End Points.


As we all know https://reqres.in is the base URL in the request which will direct us to the real Server. But as it is not yet finalized we can create a fake URL by using the below steps in POSTMAN.


There are two ways we can create MOCK SERVERS in POSTMAN.

1) Creating Mock Servers with already existing Collection

2) Creating Mock Servers from Scratch


1) Creating Mock Servers with already existing Collection


Step 1: Create a New collection. Use {{url}} in place of the base URL to create all the requests in the collection.


For GET request: To get all users list on Page 2

Set url as {{url}}/api/users?page=2



Right Click on 3 dots in front of the request and select Add example



The example will be shown below request. Click on Example.



In the body of example, we give the Expected Response. Select the appropriate response text type. Here is JSON. Also, give response status code. Here it is 200 OK.

Add Expected Response Body in Example Body.


Save the request.

Similar steps can be followed for other Get requests.


For POST Request: To add a new user

Create a new request in the collection.



Add Request Body in proper format by selecting text type. Here it's Json.



Create a new example for Post request. Add Response status code and body in an example We can generate random data using random functions provided by Postman.



Save the example and the request.

Same as a Post request we can create a Put request.

Also, a Delete request can be written with the expected response status and or body.

Step2: Creating Mock Servers with created Collection

Existing Collection in POSTMAN

Right-click on 3 dots beside the Collection name and Select Mock Collection



Give a name to the mock Server and Click on create a mock server.



A new URL will be created by postman which is our new Mock server and if we send the request to this URL, we will get the expected response.




We can also see the document for our requests and expected responses. This document is automatically generated by POSTMAN while creating a Mock Server.



New Mock Server URL will be created. We can also find URI in the document for every request in the collection.



Step 3: Checking if the mock server is giving expected responses in Postman.

Go to any request in the collection. In place of “no environment” choose the new Mock server Name which we have given.




Save and send the request. We should successfully see the response. Yay!!




Step 4: Checking if URI is giving expected responses in any Browser.


Also, if we copy and paste the URI in any Browser from anywhere, we should be able to see the response successfully.





2) Creating Mock Servers from Scratch


Step 1: Click on the New button beside My workspace.



It will show this window. Select Mock Server Option.


We can see Create a new Collection. We can add Get Request

For POST and PUT Request We can add a Request Body column By clicking on 3 dots as shown below


We can then add all the requests with request URI, Request Body (if any), Response Status code, and Response Body as expected. Click Next.


Once we see this page, We can give the Mock Server Name of our choice and click on Create Mock Server.


New Collection will be added with all requests and examples.



New Mock Server will be created with URL and documentation with all requests!!

We can use them to test our API by sending the requests to the MOCK API Server same as we have done for the mock server for the existing collection earlier in this blog.


Thus, the Mock API server will be created and can be used for testing API.


Hope you find this blog useful. Don't forget to give claps if you like it.

Happy Mocking! Keep Rocking!! 😊


49 views0 comments

Recent Posts

See All
bottom of page