Mocking in general dictionary terms means imitating or making a replica of something. So here we will replicate the server. There are plenty of other reasons and tools to work with a mock server. But Postman already has an inbuilt feature to deploy a mock server without any external installation.
What is Mock Server?
A mock server is a server that is not a real server. It is just a fake server that is simulated to work as a real server so that we can test our APIs and check the response or errors. Mock Server allows you to simulate your API data. When Mock Server receives a request, it matches the request against active expectations that have been configured. If no matches are found, it proxies the request if appropriate; otherwise, a 404 is returned.
We need a mock server for a number of reasons. A mock server is required –
- To test your own API while developing and before making that API live on the server of your organisation.
- To get feedbacks and bugs quicker.
- For checking the dependencies in your API before it is available to everyone.
- For QA engineers to use this for testing/isolating external dependencies and integration testing.
- By front end developers to use it before actual endpoints are available. This is done while designing the UI so that you don’t need to wait for the time till actual endpoints are developed. It saves a lot of time.
- For engineers to develop a prototype of their idea to present it to the investors for funding.
The mock server is a pretty important feature for a tester. It is very helpful in both the development and test phases of software.
Mocking API Data with Examples in Postman
When to use:
- Production API is not ready.
- Do not want to run requests against real data.
So, we are going to look at some real-time examples of mocking the API Data. In Postman, mock servers rely on examples saved in a collection to return mock data. Follow the steps below to get a hands-on demonstration of how mock servers and examples work together, and to learn how you can integrate them into your API workflow.
- Create a New Collection in the postman.
- Name the New Collection as ‘Mock API’. Then, add a new request to the collection.
- Name the new request to ‘GET Echo’. Here I’m going to use Demo API. The Request URL is:
- If I hit this request by clicking on “Send”, it will give me a response because it’s a valid request. So this API is present and gives me some response. Then we have to mock this data so that we can use this for any other request as well.
- First, I’m going to save this response as an example. In the response body, there is an option for saving response as an example. Click on ‘Save as example’. And name the example as ‘Example1’ and hit on Save Button. Our Example is ready now.
- Let’s go ahead and create a mock server, select a ‘mock collection’.
- This will open a window “Create Mock Server”. Enter mock server name as ‘SampleMockServer’ and click on ‘create mock server’ in the bottom right of the window. As you can see, we can send any request using the following URL, let’s copy this ‘mock URL’.
- Create a New Request now and add a mock URL followed by a simple request (get) and if I send this, the response is similar to what we saved as an example. This is how you can save your API Response as an example and use it in any other request.
So, this is one of the ways of mocking the API Data.