Productive API Mocking with the IntelliJ WireMock Plugin
WireMock open source has always provided a number of ways to run a standalone WireMock server - running WireMock Standalone or WireMock in Docker all configured via json based stub mapping files.
However, up until now, running a standalone server had been something that happened external to your IDE, via the command line or docker console. This has all changed with the release of JetBrains IntelliJ Idea 2024.1 where JetBrains provided first class support for WireMock inside their suite of IDEs with the free WireMock plugin for IntelliJ Ultimate users.
The WireMock plugin provides the following benefits to developers using JetBrains IDEs:
- Seamless Integration: The plugin integrates seamlessly into the IntelliJ IDE, providing developers with a familiar environment to work in. This integration streamlines the development process by eliminating the need to switch between different tools or environments.
- Increased Efficiency: Automatic generation of stub mapping files is now available in various places in the IDE. Alongside this, the mapping file code completion makes writing mapping files by hand much quicker and easier. Being able to start a WireMock server with a simple keyboard shortcut is a great productivity boost
- Flexibility: It allows starting, stopping, and configuring WireMock servers easily. Moreover, it provides the ability to start a WireMock server with custom command-line arguments.
Easier Collaboration: With WireMock functionality being built directly into IntelliJ Idea, teams can make use of other JetBrains tools such as Code With Me to help build their stub definitions.
In this post we will explore the features of the plugin and what it allows us to do.
Installing the WireMock plugin
The WireMock plugin is installed like any other plugin - open Settings and click on Plugins and search for WireMock. Click on the Install button and restart your IDE. That is all you need to do, the plugin doesn’t provide any configurable settings so you should now be ready to start using the plugin.
Generating Stub Mappings From Endpoints
One of the really nice features of this plugin is its ability to generate WireMock stub mapping files from the endpoints in your code. If IntelliJ Ultimate understands the framework you are using, all endpoints for your service will be loaded into the Endpoints tool window. From there you can call the endpoints via the built in http client or generate OpenAPI specification for those specific endpoints. The WireMock plugin adds a new menu item to this view to generate the stub mapping json for the selected endpoint:
Selecting the Generate WireMock Stubs menu item in the above screenshot will generate the following json mapping file:
{
"mappings": [
{
"request": {
"url": "/v1/people-in-space",
"method": "GET"
},
"response": {
"status": 200
}
}
]
}
The new stub file is saved as a scratch under Scratches and Consoles | WireMock Stubs.
Generating Stub Mappings From OpenAPI
As well as generating stub mapping files from the endpoints in your code, the WireMock plugin also provides the ability to generate mapping files from an OpenAPI schema. If you have an OpenAPI specification open in the IDE, select the triangle in the editor gutter and select Generate WireMock Stubs:
Selecting the Generate WireMock Stubs menu item in the above screenshot will generate the following json mapping file:
{
"mappings": [
{
"request": {
"url": "/v1/people-in-space",
"method": "GET"
},
"response": {
"status": 200
}
}
]
}
The new stub file is saved as a scratch under Scratches and Consoles | WireMock Stubs.
Mapping File Code Completion
The above stub mapping file is a great start to allow you to create mapping files to be used in your tests. However, it is likely that you will need to add to the mapping file. This is where the WireMock code completion comes into play. If you are adding to the auto generated mapping file or creating one from scratch If you place the json file in a folder called mappings or the file contains the "mappings" key, IntelliJ IDEA recognizes it as a WireMock stub file and provides coding assistance. Start typing to display suggestions and the corresponding documentation.
Running a WireMock Server Within the IDE
One of my favourite features of the IntelliJ WireMock plugin is that it allows you to run a WireMock standalone server directly from the IDE for the open stub mapping file. This really facilitates an API prototyping based workflow and when combined with JetBrains Code With Me, it can be an extremely powerful tool for cross team API design and development.
You start the WireMock server by clicking on the icon in the upper right of the editor window and you should see it running in the Services tool window.
Conclusion
In this post we have looked at the features of the new WireMock plugin by JetBrains and how it brings first class support for WireMock directly within your IDE. By embedding a WireMock server it reduces the friction of getting a mock server up and running for you and your team and makes writing stub mapping files much simpler with the built-in code assistance and code generation tools.
The WireMock team is excited to see where the JetBrains team takes the plugin next.
If you are looking to speed up your API development by mocking the APIs you depend on, there is a comprehensive set of stub mapping files for popular services that can be used in both WireMock open source or WireMock Cloud. Load these stub mapping files into your JetBrains IDE, start up a mock server and start building your software.
Join in the conversation - If you are a WireMock open source user, join the WireMock community slack where we chat about all things mocking.
/