Step 4: Your first Testcontainers integration
Last updated
Last updated
From the Testcontainers website, we learn that there is a simple way of running different supported JDBC databases with Docker:
An especially interesting part are JDBC-URL based containers:
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 provider
postgresql:14-alpine://
- we use a PostgreSQL database, and we select the correct PostgreSQL image from the Docker Hub as the image
testcontainers/workshop
- the host name (can be anything) is testcontainers
and the database name is workshop
. 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.
Add the following line to your ~/.testcontainers.properties
file to disable these checks and speed up the tests:
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.