I won’t use Wiremock with Spring Boot anymore for smaller simpler services.
Spring and Spring boot is my day to day tech as part of my present organisation, along with it unit/ integration testing is something which we all do for maintaining the code quality and code coverage.
Recently I was just writing few unit tests in my code, used @SpringBootTest
for the tests.
The code was nothing more than consuming the downstream endpoints using the RestTemplate
class - very straightforward. Generally teams(including mine) would make use of a mock downstream server(wiremock or something custom) to satisfy the availability of the mock endpoints during the unit/ integration test run.
So I came up with an idea of creating an endpoint corresponding to the downstream in the test package itself by creating a @RestController
class in the test package.
I would rather name the tests here as Implementation tests like a boundary between integration and unit tests, a combination of both.
Code & Config
The example for this POC is here.
It is very easy codebase to explore with & you will easily be able to get the idea about it.
An upstream controller serving the path /v1/upstream
and which is just calling another downstream.
Downstream API endpoints in the application.properties
.
IDEA of skipping away from — external mock setup.
- create a @RestController
class inside the test package and that way spring will load that inside the spring context, and these endpoints acting as downstream will be available during the test run only.
- Since the @RestController
is not in the main package it should be completely fine with actual package(jar) meant for deployment. Clean and simple.
What I mean?
- I have unit tests written with @SpringBootTest
— it triggers Spring to initiate an actual embedded server while running the unit tests.
- In the src/test/java
package, placed a @RestController
mapped class that represents mocked downstream endpoints.
- the downstream mock endpoints exposed via @RestController
in the test package, are only available during tests run context/ boundary.
- so the RestTemplate
consumes itself own running instance of embedded server during the tests and validates the integration against itself instead of firing up a downstream mock separately.
I am looking for critics to help me with the IDEA shared above. Sorry wiremock team, no hard feelings. 🙏
For any questions, queries, feedback please comment below or contact me over LinkedIn here