Here will show one example of doing integration testing taking advantage od the containers technology.
Usually for integration testing some resources need to be allocated for them, such as a database, a messaging system, etc.
With the usage of containers technology we can allocate the required resource for the test quite easy.
In this example I have created a very simple Spring Boot application that interacts with a MySQL database.
For the testing, we would need to insert some data, access to it and so.
For this purpose in our JUnit Test case we can create a MySQL database using Docker, initialize it and use it for our tests. Afterwards, once all our tests are done, the database is destroyed; freeing up the resources.
Here will use Testcontainers library, which I find very useful for this goal.
This example tries to show how to use them not interacting directly to the database container, but using Spring features such as the repositories, autowiring, etc.
Full source code (as a first version to be improved) is located in this GitHub repo:
https://github.com/rafareyeslopez/spring-mysql-testcontainers-demo