Testcontainers
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
start a real PostgreSQL for tests from code.
Planned topics
- Testcontainers starts Docker containers from JUnit tests and removes them afterwards.
@Testcontainerswith@Containermanages the lifecycle.- Spring Boot's
@ServiceConnection(Boot 3.1 or later; verify version before recording) configures the datasource from the container, with no URL copying. - Older style:
@DynamicPropertySourcesets properties from the container. - Make the container
staticso one container serves all tests in the class. - Share one container across classes (singleton or reuse) to save startup time.
Draft outline details
Demonstration
@Testcontainers
@SpringBootTest
class OrderIT {
@Container @ServiceConnection
static PostgreSQLContainer<?> db = new PostgreSQLContainer<>("postgres:16");
}Check your understanding
What does @ServiceConnection replace?
Show answer
Manually copying the container URL and credentials into properties.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.