Look, WireMock is great! But it can be much better with the proper tooling. And wait no more, seems IntelliJ IDEA 2024.2 now have a proper plugin that finally improves a lot of things around WireMock JSON stubs!
Install WireMock Plugin
It is right here, you need IntelliJ IDEA Ultimate or any other commercial edition of JetBrains IDEs.
From Server Code to Stubs
Let's open a basic Spring PetClinic code and start by creating some stubs for our application. Go to Endpoints tool window, select desired request mappings and open the context menu.
Great! Generated stubs posted to the editor:
{
"mappings": [
{
"request": {
"url": "/owners/find",
"method": "GET"
},
"response": {
"status": 200
}
}
]
}
As far as I understand, IDE uses special scratch files for that and you can go back to generated code any time:
Editing Experience
Once we get to the editor we see completion available and it also validates the correctness of the document.
Run
And what we can do right away without installing anything - start the mock server via run line marker or a floating button in the editor.
The server is bundled with the plugin so it starts immediately. Let's check if it responds via HTTP Client. It would be a simple request:
Viola! Getting 200 code in return!
Enabling Templates
There is an additional magic button in the editor you can try:
It enables Handlebars templating in your stubs files (completion, highlighting, etc). So we can write a more sophisticated stub now:
{
"request": {
"urlPathTemplate": "/owners/find/{name}"
},
"response": {
"body": "{{request.path.name}}",
"transformers": ["response-template"]
}
}
And IDE helps us with all properties of a request and available functions:
Conclusion
Not that it is a big deal as AI nowadays, but WireMock plugin for IntelliJ is a huge time-saver for me. Mock everything folks, it pays of!
Top comments (0)