In modern application development, interaction with web services has become essential. However, what happens when these services are unavailable, still under development, or simply unstable? This is where Mockoon comes in to save the day.
What is Mockoon?
Mockoon is a powerful tool for simulating web services. But before diving into its features, let’s clarify the concept:
A mock is a controlled imitation of a real component. In the context of web services, it can be an HTTP response containing mock data.
Mockoon comes in two forms:
- A Graphical User Interface (GUI): to visually configure your mocks
- A Command Line Interface (CLI): for integration into your CI/CD pipelines or deployment environments
Mockoon’s GUI
The Mockoon interface is organized around key concepts:
- Environments: Independent spaces representing one or more web services
- Endpoints: The routes of your simulated web service, each configured to respond to specific requests
- Endpoint configuration: HTTP verb (GET, POST, etc.), path, headers, and other parameters
- Response configuration: HTTP status code, headers, body content (with template support)
This interface allows you to set up a functional mock in just a few clicks, with little to no code writing required.
Feature Demonstration
Import from Swagger/OpenAPI
One of Mockoon’s strengths is its ability to import OpenAPI interface contracts. This feature allows you to:
- Import an existing specification file
- Automatically create all defined routes
- Generate coherent random data using FakerJS
- Customize these responses with the integrated templating system
Example
This OpenAPI interface contract can be imported by Mockoon, which will create HTTP routes to reproduce the behavior of a mock web service respecting the contract:
Template
The request body can be dynamically configured using a templating system. In our example, a possible configuration is:
|
|
repeat
is one of Mockoon’s helpers and allows repeating a block multiple timesfaker
is a JavaScript library used to produce mock data- the
oneOf
andarray
helpers allow selecting one element from a predefined list, to match possible enum values
Result
This endpoint now generates this type of response on http://localhost:3000/clones/
:
|
|
Data Buckets
Data buckets are data repositories that allow you to store and reuse information across different routes. They are particularly useful for:
- Maintaining data consistency between calls
- Implementing features like pagination
- Simulating complete CRUD operations
In our case, we can simply create a data bucket from the data generated on the /clones
endpoint. Then, creating a CRUD route from this data bucket opens the way to using all common HTTP verbs (GET, POST, PUT, PATCH, DELETE).
Moreover, between each action, the data bucket’s state is updated, which brings the mock’s behavior closer to that of a real web service. Restarting the environment resets the data bucket’s state.
Rules and Filters
Mockoon allows you to apply rules and filters to customize responses based on the request content:
Deploying the Mock Server
Once your mock environments are ready, Mockoon offers several deployment options:
Via npm
|
|
To deploy multiple environments at once:
|
|
Via Container
Mockoon can generate a Containerfile adapted to your configuration:
|
|
Then you just need to build and run it:
|
|
Via GitHub Actions
Mockoon also provides an official GitHub action: mockoon/cli-action@v2
Mockoon vs Wiremock
If you’re already familiar with Wiremock, you might be wondering why choose Mockoon? Here are my pros and cons:
- ✅ Faster to set up
- ✅ Easier to configure thanks to its intuitive graphical interface
- ❌ Fewer advanced features than Wiremock (but covers most common needs)
Contributing to the Project
Mockoon is an evolving open-source project. You can contribute or follow its development on GitHub.
Conclusion
Mockoon represents an elegant and efficient solution for mocking web services, accessible to both beginners and experienced developers. Its flexibility, simplicity, and power make it an essential tool in any developer’s arsenal.
Whether you’re looking to develop your frontend independently from the backend, deploy an isolated testing environment, or simply work offline, Mockoon is there to make your task easier!