Step 4: Your first Testcontainers integration
From the Testcontainers website, we learn that there is a simple way of running different supported JDBC databases with Docker: https://www.testcontainers.org/usage/database_containers.html
An especially interesting part are JDBC-URL based containers: https://www.testcontainers.org/usage/database_containers.html#jdbc-url
It means that starting to use Testcontainers in our project (once we add a dependency) is as simple as changing a few properties in Spring Boot:
If we split the magical JDBC url, we see:
jdbc:tc:
- this part says that we should use Testcontainers as JDBC providerpostgresql:14-alpine://
- we use a PostgreSQL database, and we select the correct PostgreSQL image from the Docker Hub as the imagetestcontainers/workshop
- the host name (can be anything) istestcontainers
and the database name isworkshop
. Your choice!
After adding the properties and run the test again. Fixed? Good!
Check the logs.
As you can see, Testcontainers quickly discovered your environment and connected to Docker. It did some pre-flight checks as well to ensure that you have a valid environment.
Hint 1:
Add the following line to your ~/.testcontainers.properties
file to disable these checks and speed up the tests:
Hint 2:
Changing the PostgreSQL version is as simple as replacing 14-alpine
with, for example, 10-alpine
. Try it, but don't forget that it will download the new image from the internet, if it's not already present on your computer.
Last updated