top of page
kirithika mj

Create a mock server and verify its response using an API in Postman

Click here to read more on how to create an API specification.

Attached here is the API specification file.


Save the above API specification file with .yml extension .


The following steps helps us to develop an API and mock it using a mock server.

  • Setting up a mock server

  • Create custom examples

  • Create mock responses


Step 1: Setting up a mock server

Import and upload the above specification file in postman and choose "Test suite" in the drop down. To create the mock server from the specification file, let us create an environment first.

I) On the left tab, select " Environment" -> create environment. Name it as "McDonald Mock Env".


Currently, this creates an empty environment .


II) Go to API tab and click "McDonald Menu" API . Under Overview -> Mock Server -> select "Add mock server".


III) ' Create new mock server'

  • Under 'Select collection to mock' -> choose 'McDonald Menu' -> click 'select collection and continue'.

  • Under 'Configure mock server' -> name as 'McD Mock' -> click 'add mock server'. Select the environment as 'McDonald Mock Env'. Enable the checkbox ' Save the mock server URL as an environment variable'.

  • Click on ' Create Mock Server' button.



A mock server URL will be generated. Copy the URL.


Step 2: Create custom examples

By default, all the requests will be populated with examples . When you try to click on the examples, it generates some random data based on the API specification .




To define our own examples in the response, let's edit the response body for each request.


  • For get method, I will remove the default examples and set my sample menu items and save it.


  • For POST method, edit the response body and save it.


  • For PUT method, specify the id of the item and edit the corresponding response. In this example, I have updated price of 'Ham burger' to '35'.

Note: I have updated only one item and hence only one response will be generated. For multiple responses, you can create multiple examples.

  • Custom examples doesn't apply to delete method.

Step 3: Create mock responses

Remember, we generated a mock URL in step 1. When you add endpoints to this URL, we will get responses similar to the one defined in the examples.

Mock response for GET method:

Mock response for POST method:


Incase, if you receive "bad data sent" response, go to Headers tab and add a field as


x-mock-response-code :<corresponding response code>

In my case, I got a "bad data sent" when trying to fetch response for the POST method. So, I got the response I needed by forcing the mock server .

Mock response for PUT method:



Mock response for DELETE method:



Summary:

I have walked through few details on how to create a mock server, customize examples of our choice and how to check the mock responses. A mock server defined here is a dummy server which helps a consumer understand how an api works. It doesn't do any actual changes.


Happy reading!!


Reference : API Testing and Development with Postman by Dave Westerveld

862 views

Recent Posts

See All
bottom of page