Using TestContainers for Spring Boot integration tests

It was the evening of the French-German football game for the European Championship and while watching the game using one eye, I had the time to figure out how to create an integration tests using two Spring Boot connected components.


The setup of the Spring Boot components is quite simple. There are two Spring Boot components; module1 and module2. Module1 forwards a request to module2.

http client (like curl)-> module1 -> module2

Integration test

In the integration test we want to load both container module1 and module2. The integration test calls module1 and verifies the answer.

Integration test -> module1 -> module2

The project testcontainers has a module it, which contains the integration test.

First, we need Testcontainers to be in the pom file (in short)

To load all the docker containers and have them connected, Docker compose is our friend. The docker compose file is very straightforward.

And finally, there is the integration test

The test is quite simple, first a compose environment is defined. The withExposedService methods will make sure that Testcontainers will wait until both services are up and ready.

Next, the compose set is started and finally the real test is run.

As you see, starting a set of Spring Boot micro services can be started simple using Testcontainers and Docker Compose to run a fine integration test.

Happy coding!

Java, Spring Boot and Kotlin developer. The backend guy!