Microservices are testable in isolation

Microservices are testable in isolation

One of the defining characteristics of the microservice architecture is that services are independently deployable. But there's more to this definition than simply packaging a service as a WAR or executable JAR that can be built and deployed independently of other services. A service in a microservice architecture is also testable and is tested independently of other services.

One testing challenge is how to test a service that has dependencies e.g. a service might invoke another service's REST API? The solution is to use a test double that simulates the that other service. A test could, for example, use a framework, such as Wiremock, to simulate its dependency's REST API. Wiremock is essentially Mockito for HTTP APIs.

An even bigger challenge is convincing yourself to not use end-to-end tests - at least in the deployment pipeline between developer's laptop and production. For example, it's common to implement business facing acceptance tests as end-to-end tests.

But the trouble with end-to-end tests is that are typically slow, brittle and time consuming to write. They slow down the deployment pipeline, which reduces the frequency of deployments, which results in larger batches of changes going into production. What's more, end-to-end tests increase the time needed to fix a production outage. It's much better to refactor application-level acceptance tests into service-level tests.

Sadly, a common microservices anti-pattern is (a) to have too many services and (b) test those services together. This approach defeats the purpose of using the microservice architecture to accelerate software delivery.

Services that need to be tested together should most likely be merged into a single large services, a.k.a. modular monolith. Note however, that there are exceptions to this principle. Dark energy forces, such as the need to use multiple technology stacks, such as Java and Python, might require you to have multiple services that are tested together. But as a general rule, if a service cannot be tested in isolation you must investigate why and question its existence.

Francisco Javier Gallucci Vivas

Senior Software Engineer | Ex-Intel | Solutions and cloud architect | AI rookie

2 年

You just mentioned one of the main problems I have faced (still) in my career. You mention that if two services need to be tested together, perhaps they should be a single service. Completely agree, however, why do we get to that scenario? I could say that in many cases is due to a bad decomposition. What I'm getting at is that I always suffer trying to explain how I think the decomposition should be, based on your book, it should be by subdomain or business capabilities. However, the "standard" that I get right now in the industry, and in my personal opinion, a disastrous standard, and it is what I would like to correct or reaffirm. It's that services are as atomic as possible and do only one atomic thing. That means, for example, an endpoint that does one thing and that's it. So you end up with "ecosystems" of 80 microservices + 100 topics, etc etc??♂? , and now with serverless, even worse. A decomposition masterclass or bootcamp would be great ??

要查看或添加评论,请登录

Chris Richardson的更多文章

社区洞察

其他会员也浏览了