Development
3 min readMicroservice architecture as a whole has more inbound and outbound communication than monolithic systems. Where monoliths might have one request that performs several major actions behind the scenes, microservices very easily could split that up across multiple requests. Sometimes you will need services to communicate with each other directly. So, the question becomes: how do you test communication across microservice boundaries?
Let’s say that we have two microservices: “Microservice A” and “Microservice B”. Microservice A communicates with Microservice B via a REST endpoint that is owned by Microservice B.
In this tutorial, we will review how this communication can be tested from the perspective of the calling service (Microservice A). We recommend using the MockWebServer third party library to provide the functionality for these tests.
Use Okhttp3 MockWebServer library. Include the following to your maven pom.xml:
For testing both outbound request and inbound response you will need to add and initialize mock web server property to your test class:
In the example code below, we will be attempting to apply an offer.
Full test:
Full test:
For providing testing across microservice boundaries, MockWebServer provides a simple, easy-to-implement option for those wanting to test service-to-service communications. Further information can be found directly on the MockWebServer’s Github page: https://github.com/square/okhttp/tree/master/mockwebserver.