|
| 1 | +# Step 4: Your first Testcontainers integration |
| 2 | + |
| 3 | +From the Testcontainers website, we learn that there is a simple way of running different supported JDBC databases with Docker: |
| 4 | +[https://www.testcontainers.org/usage/database\_containers.html](https://www.testcontainers.org/usage/database_containers.html) |
| 5 | + |
| 6 | +An especially interesting part are JDBC-URL based containers: |
| 7 | +[https://www.testcontainers.org/usage/database\_containers.html\#jdbc-url](https://www.testcontainers.org/usage/database_containers.html#jdbc-url) |
| 8 | + |
| 9 | +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: |
| 10 | + |
| 11 | +```java |
| 12 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = { |
| 13 | + "spring.datasource.url=jdbc:tc:postgresql:16-alpine://testcontainers/workshop" |
| 14 | +}) |
| 15 | +``` |
| 16 | +Let's apply it to the `AbstractIntegrationTest` class: |
| 17 | +```java save-as=workshop/src/test/java/com/example/demo/AbstractIntegrationTest.java |
| 18 | +package com.example.demo; |
| 19 | + |
| 20 | +import org.springframework.boot.test.context.SpringBootTest; |
| 21 | + |
| 22 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = { |
| 23 | + "spring.datasource.url=jdbc:tc:postgresql:16-alpine://testcontainers/workshop" |
| 24 | +}) |
| 25 | +public class AbstractIntegrationTest { |
| 26 | + |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +If we split the magical JDBC url, we see: |
| 31 | + |
| 32 | +* `jdbc:tc:` - this part says that we should use Testcontainers as JDBC provider |
| 33 | +* `postgresql:16-alpine://` - we use a PostgreSQL database, and we select the correct PostgreSQL image from the Docker Hub as the image |
| 34 | +* `testcontainers/workshop` - the host name \(can be anything\) is `testcontainers` and the database name is `workshop`. Your choice! |
| 35 | + |
| 36 | +## Running tests |
| 37 | +In order to run these test we'll need a Docker Engine available. To relax the system ;oad we'll use Testcontainers Cloud to spin up Testcontianers-based containers. |
| 38 | +To ebale it you need to: |
| 39 | +1. Go to the [app.testcontainers.cloud](https://app.testcontainers.cloud/) and generate the TC_CLOUD_TOKEN. |
| 40 | + |
| 41 | +2. Set the TC_CLOUD_TOKEN as environment variable: |
| 42 | +::variableDefinition[tcc_token]{prompt="What is your TC_CLOUD_TOKEN value?"} |
| 43 | + |
| 44 | +```bash |
| 45 | +export TC_CLOUD_TOKEN=$$tcc_token$$ |
| 46 | +``` |
| 47 | +3. Start Testcontainers Cloud agent |
| 48 | +```bash |
| 49 | +sh -c "$(curl -fsSL https://get.testcontainers.cloud/bash)" |
| 50 | +``` |
| 51 | + |
| 52 | +Now run the test again: |
| 53 | +```bash |
| 54 | +./mvnw clean test |
| 55 | +``` |
| 56 | +Test is green? Good! |
| 57 | + |
| 58 | +Check the logs. |
| 59 | + |
| 60 | +```text |
| 61 | +2025-10-27T21:36:55.945Z INFO 77211 --- [ main] o.t.d.DockerClientProviderStrategy : Found Docker environment with Testcontainers Host with tc.host=tcp://127.0.0.1:43387 |
| 62 | +2025-10-27T21:36:55.946Z INFO 77211 --- [ main] org.testcontainers.DockerClientFactory : Docker host IP address is 127.0.0.1 |
| 63 | +2025-10-27T21:36:56.055Z INFO 77211 --- [ main] org.testcontainers.DockerClientFactory : Connected to docker: |
| 64 | + Server Version: 28.3.3 (via Testcontainers Cloud Agent 1.22.0) |
| 65 | + API Version: 1.51 |
| 66 | + Operating System: Ubuntu 22.04.5 LTS |
| 67 | + Total Memory: 31556 MB |
| 68 | +2025-10-27T21:36:56.206Z INFO 77211 --- [ main] tc.testcontainers/ryuk:0.8.1 : Creating container for image: testcontainers/ryuk:0.8.1 |
| 69 | +2025-10-27T21:36:56.396Z INFO 77211 --- [ main] tc.testcontainers/ryuk:0.8.1 : Container testcontainers/ryuk:0.8.1 is starting: 779608b4dc49f2c37420ea0a39cc90951912fb767d7d7141c1b0ae1db1717989 |
| 70 | +2025-10-27T21:36:57.321Z INFO 77211 --- [ main] tc.testcontainers/ryuk:0.8.1 : Container testcontainers/ryuk:0.8.1 started in PT1.114889292S |
| 71 | +2025-10-27T21:36:57.521Z INFO 77211 --- [ main] o.t.utility.RyukResourceReaper : Ryuk started - will monitor and terminate Testcontainers containers on JVM exit |
| 72 | +2025-10-27T21:36:57.523Z INFO 77211 --- [ main] org.testcontainers.DockerClientFactory : Checking the system... |
| 73 | +2025-10-27T21:36:57.530Z INFO 77211 --- [ main] org.testcontainers.DockerClientFactory : ✔︎ Docker server version should be at least 1.6.0 |
| 74 | +2025-10-27T21:36:57.532Z INFO 77211 --- [ main] tc.postgres:17-alpine : Creating container for image: postgres:17-alpine |
| 75 | +2025-10-27T21:36:57.679Z INFO 77211 --- [ main] tc.postgres:17-alpine : Container postgres:17-alpine is starting: ed1a75d921ab911896763cde925724777aa6cea00700aec567d6b9a293b1e297 |
| 76 | +2025-10-27T21:36:58.939Z INFO 77211 --- [ main] tc.postgres:17-alpine : Container postgres:17-alpine started in PT1.406803125S |
| 77 | +2025-10-27T21:36:58.943Z INFO 77211 --- [ main] tc.postgres:17-alpine : Container is started (JDBC URL: jdbc:postgresql://127.0.0.1:32771/workshop?loggerLevel=OFF) |
| 78 | +``` |
| 79 | + |
| 80 | +As you can see, Testcontainers quickly discovered your environment and connected to Docker. |
| 81 | +It did some pre-flight checks as well to ensure that you have a valid environment. |
| 82 | + |
| 83 | +## Hint: |
| 84 | + |
| 85 | +Changing the PostgreSQL version is as simple as replacing `16-alpine` with, for example, `17-alpine`. |
| 86 | +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. |
| 87 | + |
| 88 | +```plaintext save-as=workshop/src/test/java/com/example/demo/AbstractIntegrationTest.java |
| 89 | +package com.example.demo; |
| 90 | + |
| 91 | +import org.springframework.boot.test.context.SpringBootTest; |
| 92 | + |
| 93 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = { |
| 94 | + "spring.datasource.url=jdbc:tc:postgresql:17-alpine://testcontainers/workshop" |
| 95 | +}) |
| 96 | +public class AbstractIntegrationTest { |
| 97 | + |
| 98 | +} |
| 99 | +``` |
0 commit comments