@@ -20,6 +20,8 @@ Although we can unit test these methods nicely, we're still going to want to bui
A common choice for this is Wiremock or MockServer, but you can actually do it all using Spring Boot's `RestClientTest`, which provides the means to [test an auto-configured, and mocked, rest client](https://docs.spring.io/spring-boot/docs/2.6.3/reference/html/features.html#features.testing.spring-boot-applications.autoconfigured-rest-client).
Sample code for this blog post can be found [on GitLab](https://gitlab.com/jamietanna/spring-boot-http-client-integration-testing).
# Base setup
Let's say that we have a class, `ProductServiceClient`, which can be described using the following interface:
@@ -23,6 +23,8 @@ A common choice for this is Wiremock or MockServer, and I'd hoped to say that, s
We can, however, use okhttp3's `MockWebServer` as noted on the issue above, and in [this blog by Mimacom](https://blog.mimacom.com/spring-webclient-testing/), which may be slightly more lightweight than our other options.
Sample code for this blog post can be found [on GitLab](https://gitlab.com/jamietanna/spring-boot-http-client-integration-testing).
# Base setup
Let's say that we have a class, `ProductServiceClient`, which can be described using the following interface:
If you're building Spring Boot services which interact with other services, it's likely that you're using the `WebClient` from the WebFlux project to use a more reactive and non-blocking HTTP client.
Although we can unit test these methods nicely, we're still going to want to build an integration test to validate that the HTTP layer works correctly.
As noted in [the version of this article, using OkHttp](https://www.jvt.me/posts/2022/02/07/webclient-integration-test/), we can't use a built-in Spring means to test this, but we can use an HTTP server like [Wiremock](https://wiremock.org/).
Sample code for this blog post can be found [on GitLab](https://gitlab.com/jamietanna/spring-boot-http-client-integration-testing).
# Base setup
Let's say that we have a class, `ProductServiceClient`, which can be described using the following interface:
If you're happy constructing an `ObjectMapper` another way, I'll leave it as an exercise to the reader, [based on how we did it for OkHttp's tests](https://www.jvt.me/posts/2022/02/07/webclient-integration-test/#setting-up-mockwebserver).
# Adding tests for multiple `WebClient` together, with custom configuration
If we want to add tests to validate that the `WebClient`s themselves are set up correctly, independent to the classes that test them, we may want to create a common test class, which can allow us to verify any configuration that has been applied to them.
Let's say that we have the following configuration class for two different `WebClient`s: