gcasar
gcasar

Reputation: 759

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?

Upvotes: 3

Views: 11302

Answers (4)

Liam Williams
Liam Williams

Reputation: 703

Another approach is to test each component in isolation and mock the dependent service on the other side of the RabbitMQ server. You can do this using an async API simulation/mocking tool.

For example you can use Traffic Parrot which can be run in a Docker container as part of your CI/CD pipeline.

Here is a video demo of how you can use the tool to send mock response messages to a RabbitMQ queue in an aysnc request/response pattern. There is also a corresponding tutorial available to follow.

Request/Response RabbitMQ pattern Recording gRPC messages Replaying gRPC messages

Upvotes: 2

erosb
erosb

Reputation: 3141

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.

Upvotes: 3

Gurinder
Gurinder

Reputation: 983

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.

Upvotes: 2

Oreste Viron
Oreste Viron

Reputation: 3805

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.

Upvotes: 4

Related Questions