If I am mocking API responses in my unit/integration tests, how do I guarantee that the contract between Service A and B are valid?

softwareengineering.stackexchange https://softwareengineering.stackexchange.com/questions/372653

Question

Let's say I have microservice A and B.

Microservice A calls microservice B for some piece of information.

Microservice B requires parameters 1, 2, and 4 for the request to be valid.

If I'm simply mocking the API call & response between Microservice A and B, how does that enforce the contract for B, in that, B requires parameters 1, 2, and 4?

Was it helpful?

Solution

If I'm simply mocking the API call & response between Microservice A and B, how does that enforce the contract for B, in that, B requires parameters 1, 2, and 4?

If you mock the test such that service A submits the right data in the right format, and processes the right result and service B accepts the expected data and returns the proper result in the right format. Since the units do what they are supposed to do, you have higher confidence that the code works properly.

To test that each unit of code’s expectations actually match, you get to wire them together and run an integration test. (Or do it manually via a smoke test). Generally, things aren’t mocked in an integration test, because that defeats the purpose.

Licensed under: CC-BY-SA with attribution
scroll top