Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring microservice end-to-end testing

I'd like to write a end to end test for a pipeline, built with spring boot.

Consider two microservices A, B where B consumes output from A and produces a RESTful API. They are connected using rabbitmq and rely on an external database.

System scheme

I would like to achieve something like:

  1. Create a new project that includes both microservices
  2. Create a test configuration that configures JPA provider to be an in-memory database
  3. Inject custom MQ into A, B to connect them (rabbitmq is not tightly coupled)
  4. Write tests

Essentially replacing the white parts with mocks and testing the coloured parts.

Does this make sense? Test coverage of A and B is not complete and such a test would guarantee that the contract between A and B holds. Are there better ways?

like image 411
Greg Avatar asked Sep 04 '25 03:09

Greg


2 Answers

If you have the time, I suggest you to read this : https://martinfowler.com/articles/microservice-testing/

The purpose of end-to-end testing is not to do 100% of line coverage.

like image 88
Oreste Viron Avatar answered Sep 06 '25 05:09

Oreste Viron


My first idea about this topic is that if it is an end-to-end test, then you should forget which framework do you use, because that relates to implementation in this context. So I would create a test project, which is essentially a docker-compose file, and defines 5 containers for

  • service A
  • service B
  • RabbitMQ
  • maybe database too, unless you want to stick to the in-memory approach
  • and a separate container for running the tests

From this perspective you have 2 ways of handling env-specific configuration:

  • you define test-specific config in a separate spring profile, and you activate it by defining the SPRING_PROFILES_ACTIVE env var in the docker-compose file
  • you pass your config in a properties file, and mount it in the docker-compose file

The test runner can be kept simple, I would write a JUnit-based test suite which uses RestAssured, or something similar.

I hope this gives a clue. Of course it is a broad topic so going into every detail doesn't fit into a SO answer.

like image 34
erosb Avatar answered Sep 06 '25 05:09

erosb