The WireMock Plugin for IntelliJ IDEA Is Here!

The scariest phrase for anyone testing web-dependent codebases is “third-party service”. In the modern landscape, it feels like almost every part of our application hinges on some external cloud-based dependency. Tests help us feel confident about our logic and that our code can handle both expected and unexpected situations. However, third-party services, and especially web APIs, can be a huge pain to set up and manage, leading many developers to skip testing critical parts of their codebase. At JetBrains, we hope that a new plugin will help you write more tests, feel more confident about implementing features, and ultimately ship better software.

In this post, we’ll take a quick look at the new WireMock plugin introduced with IntelliJ IDEA 2024.1. It works in all JetBrains IDEs and provides developers with a built-in mock server. We’ll also explore a few features that should GET you excited.

What is WireMock?

WireMock is a popular open-source tool for API mock testing that allows developers to create stable test and development environments. WireMock offers a suite of features, including JUnit support, JSON templates, and record and playback tools for easy capture and request playback. In addition to HTTP APIs, WireMock supports newer formats and protocols such as webhooks, gRPC, and GraphQL.

If your application code calls a web service of any kind, WireMock can stub out endpoints to replace those calls, helping you write better software.

IntelliJ IDEA WireMock plugin

While you could use the WireMock suite to write stubs in Java JUnit tests for your HTTP services, the WireMock mock API template option is a cleaner and more manageable approach. Templates use a JSON file format to simulate any endpoint, and there’s even a public WireMock template library for popular third-party APIs. The one catch is that you must run a WireMock server to handle user requests. Luckily, the WireMock plugin is an embedded WireMock server in your JetBrains IDE.

Let’s look at running a simple API. The popular XKCD comic has a two-endpoint API that returns the latest comic and the associated metadata. You can find the WireMock template API here. Placing the template in a mappings folder, you’ll immediately notice the WireMock server icon in the top-right corner.

Clicking the icon will start the WireMock server and attach the current template for the XKCD API. You should see the WireMock server running in the Services tool window.

To test the server is up and running, you can use the built-in HTTP Client to make the appropriate HTTP requests and view the JSON response.

If you’re an API author, the WireMock plugin also offers ways to quickly author stubs using the Endpoints tool window. Right-click any HTTP endpoint and choose the Generate WireMock Stubs option.

IntelliJ IDEA will place any generated stubs in the WireMock Stubs directory under the Scratches and Consoles section of the Project tool window. From here, you can modify the JSON template with full schema support and code completion. Similar to the previous template, you can launch these stubs using the built-in WireMock server.

Conclusion

WireMock is a fantastic option for web developers working with homegrown or consuming third-party APIs. The new WireMock plugin brings an improved workflow for working with and authoring WireMock templates, helping you deliver the quality software your users deserve.

To try it out, download IntelliJ IDEA and look for the WireMock plugin in the Marketplace tab, or install it directly from JetBrains Marketplace.

image description