I have been using Wiremock as my preferred HTTP test double for some time now. I think it is a fantastic tool and I mentioned it quite a lot at a talk I gave at
and it turned out the author, Tom
, was in the audience.
Shamefully I had a private fork of Wiremock at the company I worked for, we'd hacked away at it and added support for copying our platform headers, adding our HMAC signatures to responses etc. We'd also used it for load testing and made a bunch of the Jetty tuning options configurable. Some of this, HMAC, was confidential, 90% not so much :)
So over the Christmas holidays, with the help of Tom, I've been hacking away with Wiremock, and the new release now contains:
- Configurable number of container threads
- Exposed Jetty tuning options: Acceptor threads & Accept queue size
- Extension points
The first two were my PRs, the latter was by Tom, who (rightly) rejected my PR as it added too much latency to
start up as it was reflection based. But kindly Tom hashed out an alternative documented here: https://github.com/tomakehurst/wiremock/issues/214
If you've used Wiremock before you'll know you run/interact it in two modes: via its Java API and as a standalone
process. This means you can use it for unit/integration testing and black box acceptance testing. Let's look with
the Java API, how to use this feature in standalone mode is documented on the Wiremock site:
This is the class you extend to extend Wiremock and here is a simple implementation that copies over headers that begin with Batey, this example is inspired by a platform requirement to copy over all platform headers when dealing with requests.
Simple! Now to use it from the Java API you add the following to your stubbing:
The name, CopiesBateyHeaders, in your implementation needs to match the stubbing. We can now test a piece of code that looks like this:
For both cases: When the dependency does copy the header over and when it doesn't. Here is the test for does:
Now you're probably thinking we could have just primed this right?
Well I hate noise in tests, and we want a single test making sure we throw an error if the header isn't copied but for all the rest of the behaviour (obviously there isn't any in this example) we can now forget about the fact our dependency should copy the headers, thus reducing noise in the priming of all our other tests.
I find this particularly important in black box acceptance tests, which often get very noisy.
I love open source :) All the code for this example is on my github here