0
0
Spring Bootframework~30 mins

Test containers for database testing in Spring Boot - Mini Project: Build & Apply

Choose your learning style9 modes available
Test containers for database testing
📖 Scenario: You are building a Spring Boot application that needs to test database operations reliably. Instead of using a real database server, you want to use a test container that runs a temporary database during tests. This helps keep tests isolated and easy to run anywhere.
🎯 Goal: Create a Spring Boot test class that uses Testcontainers to start a PostgreSQL database container. Configure the test to connect to this container and verify a simple database connection.
📋 What You'll Learn
Create a PostgreSQLContainer instance with the image postgres:15-alpine
Configure the container to start before tests
Set Spring datasource properties dynamically from the container
Write a simple test method that checks the datasource URL contains jdbc:postgresql
💡 Why This Matters
🌍 Real World
Testcontainers allow developers to run real database instances in containers during automated tests. This ensures tests run consistently on any machine without manual database setup.
💼 Career
Many companies use Testcontainers for integration testing in Java Spring Boot projects. Knowing how to set up and use Testcontainers is valuable for backend developers and QA engineers.
Progress0 / 4 steps
1
Set up PostgreSQLContainer instance
Create a static variable called postgresContainer of type PostgreSQLContainer using the image "postgres:15-alpine". Initialize it with new PostgreSQLContainer<>("postgres:15-alpine").
Spring Boot
Need a hint?

Use static PostgreSQLContainer<?> postgresContainer = new PostgreSQLContainer<>("postgres:15-alpine") to create the container.

2
Start container before tests
Add a static block to start postgresContainer before any tests run by calling postgresContainer.start() inside the block.
Spring Boot
Need a hint?

Use a static block static { postgresContainer.start(); } to start the container once.

3
Configure Spring datasource properties dynamically
Create a method annotated with @DynamicPropertySource named registerPgProperties that takes a DynamicPropertyRegistry parameter called registry. Inside, set the properties spring.datasource.url, spring.datasource.username, and spring.datasource.password using registry.add with lambdas returning postgresContainer.getJdbcUrl(), postgresContainer.getUsername(), and postgresContainer.getPassword() respectively.
Spring Boot
Need a hint?

Use @DynamicPropertySource and registry.add with method references to set the datasource properties.

4
Write a test method to verify datasource URL
Add a test method annotated with @Test named testDatasourceUrl. Inject a javax.sql.DataSource parameter named dataSource. Inside, get the connection URL from dataSource.getConnection().getMetaData().getURL() and assert it contains the string "jdbc:postgresql" using org.junit.jupiter.api.Assertions.assertTrue.
Spring Boot
Need a hint?

Write a @Test method that gets the connection URL from the injected DataSource and asserts it contains "jdbc:postgresql".