Real Dependencies with Testcontainers
Test against real databases in containers.
Real Dependencies with Testcontainers is a free Spring Boot 4 Microservices & REST APIs lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Spring Boot 4 Microservices & REST APIs learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Testcontainers
Testcontainers runs real services — PostgreSQL, Redis, Kafka — inside Docker containers during tests.
You test against the real database, not an in-memory substitute, catching dialect and behavior differences.
Adding the Dependency
Add the Testcontainers JUnit and database modules to your build.
// build.gradle
testImplementation "org.testcontainers:junit-jupiter"
testImplementation "org.testcontainers:postgresql"The @Testcontainers Annotation
@Testcontainers on the test class activates container lifecycle management.
@Testcontainers
@SpringBootTest
class UserRepositoryIT {
// containers declared here
}Declaring a PostgreSQLContainer
A static @Container field defines the database image to start.
@Container
static PostgreSQLContainer<?> postgres =
new PostgreSQLContainer<>("postgres:16")
.withDatabaseName("testdb")
.withUsername("test")
.withPassword("test");Wiring the Datasource
Use @DynamicPropertySource to inject the container's generated JDBC URL into Spring.
@DynamicPropertySource
static void props(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url",
postgres::getJdbcUrl);
registry.add("spring.datasource.username",
postgres::getUsername);
registry.add("spring.datasource.password",
postgres::getPassword);
}A Full Repository Test
Now repository tests run against real PostgreSQL.
@Test
void savesAndReads() {
User saved = repository.save(new User("Alice"));
assertThat(repository.findById(saved.getId()))
.isPresent();
}Static vs Instance Containers
A static container starts once for all tests in the class (faster). A non-static container restarts for each test (more isolation).
Other Container Types
Testcontainers has modules for many services.
@Container
static GenericContainer<?> redis =
new GenericContainer<>("redis:7")
.withExposedPorts(6379);Reusing Containers
Enable container reuse to speed up local runs across test executions.
@Container
static PostgreSQLContainer<?> postgres =
new PostgreSQLContainer<>("postgres:16")
.withReuse(true);Spring Boot Service Connections
Spring Boot 3.1+ can auto-configure the datasource from a container with @ServiceConnection, removing the need for @DynamicPropertySource.
@Container
@ServiceConnection
static PostgreSQLContainer<?> postgres =
new PostgreSQLContainer<>("postgres:16");Docker Requirement
Testcontainers needs a running Docker engine on the machine or CI agent. Without Docker the tests fail to start containers.
Quick Check
Test your understanding of Testcontainers.
Recap
You learned to test against real dependencies:
@Testcontainers+@Containerstart Docker servicesPostgreSQLContainerfor a real database@DynamicPropertySourceor@ServiceConnectionwires the datasource- Requires a running Docker engine
Frequently asked questions
Is the “Real Dependencies with Testcontainers” lesson free?
Yes — the full text of “Real Dependencies with Testcontainers” is free to read here on the web, and the Spring Boot 4 Microservices & REST APIs course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Spring Boot 4 Microservices & REST APIs course, upgrade to CoddyKit PRO.
What will I learn in “Real Dependencies with Testcontainers”?
Test against real databases in containers. You practise Spring Boot 4 Microservices & REST APIs with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Spring Boot 4 Microservices & REST APIs?
No prior experience is required. Spring Boot 4 Microservices & REST APIs on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Real Dependencies with Testcontainers” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Spring Boot 4 Microservices & REST APIs lesson?
Yes. Every Spring Boot 4 Microservices & REST APIs lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Unit Testing with JUnit and Mockito
- Web Layer Tests with MockMvc
- Integration Tests with @SpringBootTest
- Real Dependencies with Testcontainers