Mock Servers in Postman

Reading Time: 4 minutes

If we do not have a production API ready, or you do not want to run your requests against actual data,we can make requests that return mock data defined within Postman . By introducing a mock servers and adding examples to our requests, we can reproduce the behaviour of a real API.

Basically Postman matches the request configurations to the examples we saved with request and responses when we send a request to mock servers

A mock server behaves like a real server and uses fake APIs, of course, for testing and development purpose.Such requirement is in Agile methodology where testing and development goes side by side. For this, a tester needs to have same requirement as the developer to work simultaneously. For which you need a mock server.

Postman lets you create two types of mock servers: private and public.

Private mock servers require users to add a Postman API key in the request header. x-api-key:<your postman API key>

Public mock servers can be accessed by anyone. Users do not need to add a Postman API key when sharing a public mock server.

Steps to create a new mock server:

1.Click New at the top left of Postman and choose Mock Server in Create New.

  • Choose create a new collection .

From the Postman , click Create a mock server.

A new panel will open which will enable us to create requests.

There are different columns in this panel which stands for:

  • The first column Method is for the request type methods like GET, POST,etc.
  • The second Request Path will create the URL for API.
  • Response code will define the code we wish to get in response .
  • Response Body will have the response body that we want to show.

Click on Next.

Now we can name our mock server.

Now the screen will show you the URL through which we can access the server. We can see it will be be created successfully.

Click on Close and close the window.

We can see a new collection with the same name is present with our APIs .

 You can also notice that a new environment is present in the environment variables. (https://learning.postman.com/docs/sending-requests/managing-environments/)

Select the first request in the collection and we can check our URL value by pointing the {{url}} written in the request.

As we select our environment, the {{url}} will be showing us the the created environment.

Now we can send the request and we will get the same response and status code as we created while setting up the mock server.

We can view and search the details of calls to your mock servers using the mock call log. Open a mock from the Postman app by clicking it in Collections, in APIs, or by switching to Browse and then Mocks and clicking the mock name. Our mock call log will open in the web dashboard—you can also open it from the collection in the browser, by selecting Mock Servers.

Mock List Browse View

The mock call log lists an overview of calls made to the mock url, together with request and response details.

Mock Call Log List

Mock call log entries will give the time of request, the request method , path, and a response overview. Click an entry to see more detail on request headers and body, or response headers and body. You can drill down into response data returned by a mock call.

Mock Call Log

Use the search field to find particular calls, and the refresh button at the top of the list to view updated requests.

References:

https://learning.postman.com/docs/designing-and-developing-your-api/mocking-data/setting-up-mock/


Knoldus-blog-footer-image

Leave a Reply