简体   繁体   中英

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.

系统方案

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?

I would recommend you to use Spring-cloud-contract . It helps you in maintaining contract between your microservices(Producer-Consumer Contracts). It's available for both HTTP based and event-based communication.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM