@Testcontainers enables JUnit Jupiter to manage Testcontainers declared in a test class. It discovers fields annotated with @Container and controls their startup and shutdown. section 14.1.5
Configure the container field according to the desired test lifecycle:
- Declare a static @Container field to share one KafkaContainer across all test methods; it starts before the first test and stops after the last. section 14.1.5
- Declare a nonstatic @Container field to start and stop the container for each test; use this only when test isolation requires it. section 14.1.5
Listing 14.22 Integration test with Testcontainers annotations
@Testcontainers #1
class StreamsCountHoppingWindowIntegrationTest {
@Container #2
private static final KafkaContainer kafka =
new KafkaContainer(
DockerImageName.parse("confluentinc/cp-kafka:7.5.1")); #3
A Kafka integration test combines @Testcontainers with a static @Container KafkaContainer configured from a Docker image name. section 14.1.5
