Wiremock & JUnit 5: How to use it

Daniel S. Blanco
2 min readFeb 13, 2023

--

Today we are going to see a simple post about how to configure Wiremock with JUnit 5. Something simple but different from how it was with JUnit 4 rules. For our example we will use the following versions:

  • junit-jupiter-api:5.9.2
  • wiremock-jre8:2.35.0

To begin with, if we want to use Wiremock in a very basic way, we will simply use the @WireMockTest annotation. Through its attributes, we will be able to modify the following aspects:

  • httpPort: To indicate on which port we will be able to make HTTP calls.
  • httpsEnabled and httpsPort. To indicate that we want to make HTTPS calls and in which port.
  • proxyMode. To emulate a domain name other than localhost. In this case and using HTTPClient we must use the useSystemProperties method when creating the client.

In the next example, if we configure all the attributes. We wil can invoke to a domain name other than localhost with HTTPS. For this last one we will no longer need to create a self-signed certificate as we did in previous version.

And although with very little we can already do a lot. There may be cases where we need a little more configuration. To be able to create invocations as we did before with the JUnit 4 rules and to have access to the wireMockConfig method.

This can be done through the JUnit5 extensions. But the instance that we create will be the same that we must use to create the different stubs. The benefit of this approach is that it also allows us to create different instances of the extension and we can use both at the same time.

Finally, if this approach is ideal for your tests. But you do not want to be indicating the instance in all the WireMock methods. We can indicate that the instance is created statically and it would be the same way as when we used the JUnit 4 rules. For this we would only have to use the configureStaticDsl(true) method.

I hope this post has been useful and will help you in updating your test software.

--

--

No responses yet