Postman Mock Server allows you to simulate API endpoints, enabling you to develop and test your applications without needing a real backend.
Setting Up a Mock Server
There are two simple methods to set up a mock server:
Method 1: Creating a Mock Server from Scratch
First, we have to enable the mock server feature in Postman by going into the apps. Then as shown in the image below select the mock server and click on "Create mock server".
As seen in the picture now let's create a new collection in the mock server here we can enter the request we want to mock and we can also add a request body by clicking on the dots icon.
Request Method: In this field, one can choose the action they want to perform like post, get, put patch, and so on.
Response Code: This is the field where we can choose or assign the response code for each request.
Request URL: Use the mock server URL in your application or during testing to make API calls. The mock server will return the responses you defined for each endpoint.
Response Body: Here is the field where one will write the message or response body for that particular request.
As above in the image I have created two requests for the mock server with the post method one is for success and the other is for failure and clicked on next.
Now enter the name in the "Mock Server Name" field.
Next should select the Environment we can choose one from the dropdown or select the check box to save the mock server URL as a new environment variable.
This will create a new environment containing the URL.
Make a mock server private this option is enabled for the paid plan
Then click on the Create Mock Server Button.
Now copy the URL of the mock server which was created by the postman as shown above in the image which will act as a URL for this server to test the request.
Now let's create a collection as created while creating the mock server for example I have created two post requests so I will create two post requests in my collection too and select the environment if you have any or
if you have selected the mock server as your environment variables then select the mock server as your environment as you can see in the above picture.
So as we created a collection with the post request here we used the URL that we copied earlier after creating the mockserver. This URL will act as the base URL and add endpoints created during the mock server setup (e.g., /success, /failure). now let's save and run the request then in our console you can see that the status is "200 ok" and with the message " User logged in successfully ".
If you prefer the output in JSON format, ensure the response body is formatted accordingly.
Method 2: Creating a Mock Server from an Existing Collection
As seen in the pictures below select the "collection" that you want to mock and click on the "mock collection".
Then it will redirect to the "create mock server page " where you have to mention the name of the mock server and environment, as the collection is already selected then click on "create mock server ".
Now as seen in the image this URL will act as the mock server URL for the collection you have selected.
Let's go to the selected collection [e.g. new collection] then set the environment accordingly which you have chosen while creating the mock server [e.g. mock new collection]. now if you want the same request with the actual response and headers then save the request as "save as example " as shown in the above picture
As seen above the examples for the 3 requests are created and the"{{ url }}" is now changed to the "url" which was created by the postman while creating the mock server for this collection. If you run the request now they will give you the response as the actual API as you can see in the above picture for the put request it updated the username from "Dimple" to "ambike". In this way, you can test the collection in as many ways as you need without disturbing the main API when the API is down or when the API is not responding.
Editing Mock Server
If we want to edit the mock server then select the mock server you have created [e.g. mock new collection1] and click on "Edit Configuration".
Then here you can change the mock server name or environment.
If you have any changes in your response matching like requests or headers.
Here one can change the header according to their requirements and click on "update mock server "It will update your mock server with the new changes.
Conclusion
Postman Mock Server is an invaluable tool for developers, offering several key benefits that enhance the development and testing process:
Decouples Frontend and Backend Development:
Frontend developers can work independently of the backend, allowing for parallel development and reducing dependency bottlenecks.
Facilitates Early Testing:
Enables developers to create and test APIs before the backend is fully developed, ensuring that potential issues are identified and resolved early.
Enhances Team Collaboration:
Teams can share mock server URLs and example responses, ensuring consistent understanding and implementation of API contracts.
Improves API Design:
Provides a platform to prototype and iterate on API designs quickly, ensuring the final API is robust and well-thought-out.
Speeds Up Development:
Reduces waiting time for backend development and testing environments, accelerating the overall development cycle.
Simulates Real-world Scenarios:
Allows for the simulation of different scenarios, including success and failure responses, helping to test the application’s behavior under various conditions.
Enhances Testing Efficiency:
Automates the testing process by providing predefined responses, making it easier to validate API behavior without setting up complex backend systems.
Supports Continuous Integration:
Integrates seamlessly with CI/CD pipelines, ensuring that automated tests run smoothly with reliable and consistent responses.
By incorporating Postman Mock Server into your development workflow, you can ensure a more efficient, collaborative, and proactive approach to API development and testing. This leads to higher-quality APIs and a smoother development experience, ultimately resulting in more robust and reliable applications.