User Guide: How to Create a Mock Server with Postman

When testing APIs, there’s often a need to create a mock server due to business requirements. So, how do you create a mock server using Postman? In this article, I'll show you how to create a mock server using Postman.

When testing APIs, there’s often a need to create a mock server due to business requirements. So, how do you create a mock server using Postman? In this article, I'll show you how to create a mock server using Postman.

What is a Mock Server?

A mock server is a virtual server environment that mimics a real server. The main reasons for using a mock server in API testing are:

  • Test Even if the Actual API Server is Incomplete
    You can test APIs by setting up a mock server even if the API is still under development.
  • Easily Respond to API Specification Changes
    Mock server implementations can be changed freely, making it easier to respond to changes in API specifications.
  • Not Affected by Communication Errors and Delays
    Since no actual communication is involved, testing can be done unaffected by network conditions.
  • Easy for Load Testing
    Performance testing is easier because requests can be accepted stably even when a large number of requests are sent.
  • Easy to Build Automated Tests
    By defining expected inputs and outputs in advance, you can run tests automatically.

By using a mock server in this way, API testing becomes easier and development efficiency is improved. It's advisable to utilize it throughout the entire testing process for optimal results.

About Postman's Mock Servers

Postman is a very popular API testing tool and it comes with built-in support for mock servers. Postman's mock server can simulate the behavior of real servers, making the testing process more efficient.

Key Features of Postman Mock Servers

Postman’s built-in mock server provides a very useful function that enhances API testing efficiency.

  • Easy to Implement: Launch a mock server and prepare API requests with just a few clicks using Postman.
  • Dynamic Responses: Use JavaScript in pre-request scripts to dynamically create response data.
  • Multiple Mock Samples: Generate multiple mock samples for the same API endpoint. This allows developers to test different scenarios and responses.

How to Use Postman's Mock Servers

So, how can I use a mock server in Postman? Below are the steps to easily set up a mock server in Postman.

Setting Up a Mock Server

  • Create a Request: In Postman, create a request and save it to any API in your collection.
  • Save the Response: After sending the request, save the returned response as an example.
Save Response.png
  • Create a Mock Server: Click the "..." next to the collection in the sidebar and select "Mock Collection." Then enter a name and click to Create a Mock Server.
Create Mock Server.png
  • Swap URLs: Copy the Mock URL provided by Postman and replace it with the Base URL.
Swap URLs.png
  • Testing the Mock Server: By clicking the "Send" button, Postman will return the previously saved example response from the mock server.
Test Mock Server.png

Advanced Settings for Mock Servers

  1. Naming: Provide a name for the mock server.
  2. Delay Settings: Set the response delay time for the mock server.
  3. Privacy Settings: To make the mock server private, add your Postman API key to the request headers before sending requests to the mock server.
Configure Mock Server Details.png

EchoAPI: Offering a More Robust Mock Server

If you're looking for API mocking, EchoAPI is definitely helpful. The built-in mock server allows you to simulate real data through very intuitive operations. EchoAPI’s mocking functionality allows you to perform no-code mocking, so you don’t have to wait for the backend API development to complete.

Quickly Mocking

When you open "Data Schema" and enter a value with a dollar sign ( $ ), it will prompt you with related mock function options for you to choose from.

image.png

Then, when you open the"value Tab", clicking "Generate from Schema" will allow you to generate mock data.

image.png

Advanced Mocking

Sometimes, when the mocking requirement is very complex you need to use advanced mocking. Expectations is available for advanced mocking features.

Expectations

Expectation Conditions: Return different data based on parameters of different requests. For example, create the following two works:

  1. Return "order1" as the sale numbers when the request parameter "quantity" is 1.
  2. Return "order2" as the sale numbers when the request parameter "quantity" is 2.
image.png


The essence of expectation is "when it matches a mock rule, it returns the response set in the rule." To achieve the above goals, we need to create two new expectations through advanced mocking.

  1. Return "order1" as the sale numbers when the request parameter "quantity" is 1.
image.png
  1. Return "order1" as the sale numbers when the request parameter "quantity" is 2.
image.png

Note: When creating new expectations, you can also set parameters for Query, Header, Cookie, and Body. You can also set conditions such as equal to, less than, greater than, and exists.