Testing an API is important, but you can’t always rely on having access to a working version of the API you need to test. Often, these different parts of a system are developed at slightly different times, so you might be working on a UI element that needs certain data through the API before that data is ready. In that case, you can still test your API by using mock or fake data.
Let’s explore how to create a mock using Postman.
Postman Tutorial Chapters
- Overview – The Ultimate Postman Tutorial for API Testing
- Chapter 1 – Getting started with Postman for API Testing
- Chapter 2 – Understanding API Authorization Options in Postman
- Chapter 3 – Using Postman for Automation Testing
- Chapter 4 – Using Test Scripts in Postman
- Chapter 5 – Running Postman in CI using Newman
- You’re here → Chapter 6 – Creating Mocks with Postman
What is Mock Data?
Mock data is essentially hard coded data that will be returned when a certain endpoint is called with given parameters. It will generally be less complete than the real system, but it has some flexibility advantages. You can, for example, setup your mock data to return errors or create corner cases that might be hard to replicate otherwise. The ability to generate this kind of data is a great tool for a tester to have. As we would expect, creating data like this in Postman is very easy.
Let’s take a look at the jsonplaceholder API again. We already have a request that gets us the data for a given post ID, so let’s take a look at how we could mock that endpoint. Click on the view more actions menu, and choose the Mock Collection option. This will prompt you to create a mock server. A mock server is needed to process and respond to the requests that you will make and to send back the correct mock data. Give the mock server a name and click on the Create Mock Server button. This creates a simple server that you have access to. You can copy the Mock URL, but at this point it won’t do much since you have not put any mock data into the server yet.
Create Mock Examples
In Postman you add mock data to a mock server using examples. In order to do that, go to the jsonplaceholder request that you created earlier in this tutorial and click on the examples button at the top right of the tab.
Click on the Add an Example button and then give your example a name. If you still have the post_id variable in your call you will need to remove it and replace it with an actual post ID like 1. This is so that the mock server is able to resolve the request that you send.
Now you need to setup the example response. This response is the mock data. You can put whatever you want in here as your response. Just to see that it works, let’s type It works! in the body field of the example response. Let’s assume for now that we want to have this as a successful call, so put 200 OK in the Status field Save the example with the Save Example button.
You now have a mock server and an example with mock data that the server can access. The next step is to make a call to the mock server.
Using a Postman Mock Server
Calling a mock server is as simple as replacing the server URL with the mock server URL. In this case, the server URL is https://jsonplaceholder.typicode.com and so everything after that (/posts/1) is the resource path. You need to replace the server URL with your mock server URL. If you need to find the mock server URL, you can click on the arrow beside your collection and go to the Mocks tab to see the current Mock server URL.
If you use that server URL and add the resource endpoint you should end up with a URL that looks something like this:
https://05560e7b-a44d-4d61-a043-9345dc70b998.mock.pstmn.io/posts/1
You can paste that URL into the address bar of your browser and you should see a response that says “It Works!“
From here you can modify the example to respond with other status codes or other data in the body. You can set up the example to respond with different header types as well. In addition, you can create more examples that respond with different kinds of data so that you could do the testing or design that you wanted to.
Conclusion
This tutorial has taken you through all of the greatest benefits of API testing with Postman. If you want to continue to learn more about how to use Postman for API testing, I would encourage you to get hands on experience. Start using Postman to explore APIs of the application you are currently testing. See if you can automate some tests. Set Newman up and try it locally. Try to get Newman to run a couple of tests in your CI system. Create a mock server and use that to test the UI. Make sure to successfully complete all of the steps that we have gone over in this tutorial and when you get stuck on something, check the Postman documentation, search google, look on StackOverflow and ask for help.
It might feel a bit overwhelming at first, but if you stick with it, you will be automating your API testing using Postman before you know it 🚀
Already started your hands on experience with Postman – Share it in the comments below! 👇
For post request, when we create example in postman – Headers will not be captured in examples?