Tuesday, January 6, 2015

Wiremock: Now with extension points (open source == awesome)

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 Skills matter and it turned out the author, Tom Akehurst, 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:

And doesn't:

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.

1 comment:

Álvaro said...


At first thanks for the post, it is very usefull!! :)

I have one question for you.
I start my wiremock server as

java -cp "wiremock-extension-example.jar:wiremock-standalone-2.6.0.jar" com.github.tomakehurst.wiremock.standalone.WireMockServerRunner --port 9001 --verbose -- extensions info.batey.wiremock.extension.CopiesBateyHeaders --global-response-templating

And it works properly but... now?

How can I try the class extended does effect in my request?

I tried call to http://localhost:9001/anything/Bateyservice or http://localhost:9001/anything/Batey or http://localhost:9001/Batey

And i cant see any response modificated... what is the answer expected ??

Thanks and regards