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
Comments