Mocking API using Postman

0

We have already discussed in an earlier post, what is Postman and how you can achieve Automation & create beautiful web-viewable documentation using Postman. Let’s get started and learn how we can test our APIs by mocking using Postman.

What is Mocking?

Mocking is one particular technique to allow testing of a unit of code without being reliant upon dependencies. Generally, what distinguishes mocking from other methods is that mock objects used to replace code dependencies will allow setting expectations. In Short, Mocking is creating objects that simulate the behavior of real objects.

What is the goal?

Delays on the front- or back-end make it difficult for dependent teams to complete their work efficiently. Postman’s mock servers can alleviate those delays in the development process. Developers can view potential responses, without spinning up a back end.

Rapid development!

Why do we want to do this?

By creating a mock collection in the earlier phases of API development stimulate clear communication among team members and aligns their expectations.

As a matter of fact, all teams in the development process can work in parallel and dependent teams encounter fewer delays.

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. 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.

How to create a mock server using Postman?

You can create a mock by clicking on New Button in the toolbar Header or you can also see an option on the launch screen when you launch a postman.

We will follow the following steps to integrate mock into your existing collection and use Postman Echo service to make sample API calls. :

  1. Sending a request (R1)
  2. Saving the request (R1) to a collection (C1)
  3. Saving the request R1’s response as an example (P1)
  4. Creating a mock (M1) for the collection (C1)
  5. Sending a request using the mock server (M1)
  6. Using query params to match

Step 1: Sending a request (R1)

Using the Postman app, send a GET request to the URL https://postman-echo.com/get?test=123. The response can be seen in the image below.

Step 2: Saving the request (R1) to a collection (C1)

Hit the Save button to open the SAVE REQUEST modal. You can save a request to an existing collection, or save it to a new collection. Let’s create our new collection called C1 which will be accessible in the Collections tab.

Step 3: Saving the request R1’s response as an example (P1)

Save an example response from the request R1 by hitting the Save Response button.

This takes us to the Examples screen which can be used to save the request response as an example. Let’s call this example P1.

The request method, URL, and status code are important in evaluating which responses will be returned by the created mock. Verify that all of these elements are as desired and press the Save Example button. To return to the request builder, hit the back arrow in the top left and we can now see the example we created in the top right, added to the request.

Step 4: Creating a mock (M1) for the collection (C1)

From the Postman app, click on the arrow (▸) next to the collection C1 to expand the details view. To open the MOCK COLLECTION modal, under the Mocks tab, click on Create a Mock server button. Here you can choose the appropriate environment to include in your mock or select the mock server type either private or public by default.

Once you mock the collection, the collection details view will be visible under the Mocks tab. You can also see the mock URL for the next step that we will need.

Step 5: Sending a request using the mock server (M1)

Now that we’ve designed our mock M1, let’s try sending a request to this mock endpoint. Copy the mock URL and paste it into a new request from the mock we created in the previous step.

Responses returned by the mock service totally depend on your saved examples and the included URL and request method type. We have a saved example with the path /get and the request method GET. So sending a GET request to the/get will return a proper response we are looking for.

Step 6: Using query params to match

The functionality of Postman’s Mock server is enhanced to return various responses based on matching query parameters. The mock server looks at the query params for matching requests to the saved examples. Which means when you have examples that vary only in query params and you want to mock different responses on the same request path for different query params, the mock server of Postman will return the exact answer that matches the request path and the corresponding query params.

Example as illustrated by Postman Team

And we’re done! We have walked through how to create a collection, save requests, save examples, create a mock, and use a mock.

Postman account gives you a limited number of free mock server calls per month. You can check your usage limits in the Postman API or in the account usage page.

What am I missing here? Let me know in the comments and I’ll add it in!

Leave A Reply

Your email address will not be published.