Managing a microservice code with unit tests and testing it in a live-like test environment with other components of the application is good practice, but sometimes it’s just not enough.

Unit tests are great; they help you to write better structured code that is easier to maintain and understand. Unfortunately it can happen that a service with a good unit test coverage doesn’t actually start when deployed.

Testing in a live-like environment is also a useful approach. In these tests the service interacts with other microservices, the monolith (of course), a real database, messaging queue, etc. — exactly as in production. As the complexity of an application increases, such environments get less stable, more difficult to maintain and eventually become a bottleneck for Continuous Delivery.

It’s usually quite difficult to test all the use cases in this manner, not to mention corner cases, error handling, etc. As a result, many components of the application don’t have a good set of regression tests, which makes it daunting to change and improve them.

Testing in a small & isolated environment

What if we create a small, fully-isolated test environment for our service that is completely under our control? This way we could simulate more use cases by interacting with all the involved components directly. For example, we could:

  • directly send any requests to the service being tested
  • save test values into the database or verify the values stored by the service
  • publish test messages to a RabbitMQ queue or Kafka topic directly and check how the service handles them -or- consume and verify the message produced by the service
  • replace some services with mocks and make them respond with custom data, respond with a delay, refuse connections or verify requests coming from the service under test

With Docker, it should be easy to create such environments. Luckily, most of the popular services like MySQLRabbitMQMockserver, and many others have ready-to-use images in Docker Hub and there is a fantastic docker-compose tool which we can use to start multiple containers at once.

Image for post

#docker-compose #microservices #functional-testing #docker

Functional Tests for Docker Microservices
3.45 GEEK