Calling APIs is business as usual. Building API clients is always good fun, testing them is somewhat trickier.
Wiremock is one of the most frequently used tools to mock APIs. Setting it up for testing purposes in your Spring Boot application is a bit daunting. Or should I say: WAS daunting. Enter WireMock Spring Boot, making your life easier.
I’ll kick off with building a simple API client and adding WireMock Spring Boot in the mix. Although simple, there are some gotchas as usual.
As a wrap-up, we’ll go for some practical tips on how to test a third-party client. Expect some tips on how to figure out what calls are done and how to turn this in some nice tests.
Devoxx Belgium
General info
- date: 2 - 6 October 2023
- Event information: available on The Devoxx Belgium website
- format: Tools In Action - 30 minutes
- slides: PDF
Offline questions and remarks
Why invest time in Wiremock if you have testcontainers?
Indeed, why mock when you can have the real thing, right? Here are some reasons why you still want to invest in Wiremock
- Testcontainers are only available from Spring Boot 3.1. If you are working on an older application, then chances are that you haven’t upgraded to the latest and greatest. Wiremock is a great alternative then
- Your build infrastructure might not support testcontainers, not have enough resources to run containers, or this functionality is just not available during build time.
- Wiremock also helps while developing locally, not only during testing. It’s also easy to startup and does not require a lot of resources
Do you need wiremock for unittesting when the tester/QA engineer/… is using mocks?
Using mocks for unittests does not exclude your tester from using mocks as well, these are not mutually exclusive.
Unittests focus on checking if the implemented business logic works as expected. A QA engineer tests real life scenarios, and your code might be a part of that (Integration test if you like). If there are multiple scenarios, then you need to mock these situations with the appropriate data/responses.
Wiremock studio discontinued
Although useful, it is not encouraged to use Wiremock studio. Oleg Nenashev from the wiremock project says:
This proprietary product has been discontinued for more than one year, and the recent security advisory announce a severe security issue that will not be fixed.
If you want something more visually to deal with the admin API, you can always import the open API specs in postman or insomnia.
Socials
Next week @pjdrouillon will show how to speed up writing tests with WireMock and Spring Boot! Join him for his Tools-in-Action session at @Devoxx BE: https://t.co/kYU0z8c1fE pic.twitter.com/b5THMcl4zA
— WireMock (@wiremockorg) September 27, 2023
Speed up writing tests with Wiremock Spring Boot by @pjdrouillonhttps://t.co/7EuFfKUOwW
— Elias Nogueira 🇧🇷🇳🇱 (@eliasnogueira) August 31, 2023