Testcontainers container lifecycle management using JUnit 5
In this guide, you will learn how to:
- Start and stop containers using JUnit 5 lifecycle callbacks
- Manage containers using JUnit 5 extension annotations (
@Testcontainersand@Container) - Share containers across multiple test classes using the singleton containers pattern
- Avoid a common misconfiguration when combining extension annotations with singleton containers
Prerequisites
- Java 17+
- Your preferred IDE
- A Docker environment supported by Testcontainers
NoteIf you're new to Testcontainers, visit the Testcontainers overview to learn more about Testcontainers and the benefits of using it.
Modules
- Create the project
Set up a Java project with a PostgreSQL-backed customer service for lifecycle testing.
- Lifecycle callbacks
Manage Testcontainers container lifecycle using JUnit 5 @BeforeAll and @AfterAll callbacks.
- Extension annotations
Manage Testcontainers container lifecycle using @Testcontainers and @Container annotations.
- Singleton containers
Share containers across multiple test classes using the singleton containers pattern.