What is Service Testing?
Microservices architectures enable software components to be developed, deployed, and scaled independently. While microservices applications can be tested with end-to-end tests like monoliths, it contradicts the promise of microservices -- achieving high velocity via independent development and deployment of services.
Instead, services can be tested independently by focusing on the following:
- Business functionality of the service
- Interaction with databases and messaging systems
- API contract and functionality of services (‘producer services’) that the service depends on
- API contract and functionality the service exposes to other services (‘consumer services’)
However, this is not easy. Validating the required business functionality of a service often requires interactions with other services (‘producer services’). The service functionality can’t be tested unless the producer services are available.
Solution: Accurate mocks for producer services
The solution is to accurately mock the producer services for testing the service under development. With accurate mocks for the producer services, all the capabilities of a service can be tested independently, enabling developers of a service to validate their service even before submitting the code. The same tests can be added to CI to protect against regressions going forward.
Developers also need to protect the APIs against regression for the services that rely on them (the ‘consumer services’). In this context, that means service testing should ensure that the mocks being used by the consumer services remain valid.
Microservices developers can adopt either of the following approaches:
- Maintain backward compatibility for APIs: That means existing mocks will remain valid even if API functionality is updated. The consumer services do not need to take any action with their service tests until they want to utilize the updated API capabilities.
- Maintain updated mocks for consumer services: If backward compatibility is not maintained during API development, then a process has to be implemented that updates the mocks whenever the underlying API is updated. Otherwise, the consumer APIs will pass the service tests, and fail later in the end-to-end tests.
To summarize, service testing should enable service developers to:
- Test their service using accurate mocks for their producer services
- Ensure that service mocks used by their consumer services remain valid