Skip to content
Ameen Abozaid
Planned lessonJNR-045-L02

Testcontainers

  • Lesson 2 of 7
  • Junior
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.
  • @Testcontainers with @Container manages 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: @DynamicPropertySource sets properties from the container.
  • Make the container static so one container serves all tests in the class.
  • Share one container across classes (singleton or reuse) to save startup time.
Draft outline details

Demonstration

java
@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.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.