Adding automated microservice tests in isolation
Before we wrap up the implementation, we also need to write some automated tests.
We don’t have much business logic to test at this time, so we don’t need to write any unit tests. Instead, we will focus on testing the APIs that our microservices expose; that is, we will start them up in integration tests with their embedded web server and then use a test client to perform HTTP requests and validate the responses. With Spring WebFlux comes a test client, WebTestClient
, that provides a fluent API for making a request and then applying assertions on its result.
The following is an example where we test the composite product API by doing the following tests:
- Sending in
productId
for an existing product and asserting that we get back200
as an HTTP response code and a JSON response that contains the requestedproductId
along with one recommendation and one review - Sending in a missing
productId
and asserting...