KafkaContainer provisions a Kafka broker in Docker so an integration test can run Kafka Streams against a real, disposable service. A test class combines @Testcontainers with a @Container KafkaContainer field, then supplies kafka.getBootstrapServers() to the application, producer, and consumer configurations. section 14.1.5
Configure the container and its test lifecycle deliberately:
- Construct it with DockerImageName.parse("registry/name:tag"), such as confluentinc/cp-kafka:7.5.1. section 14.1.5
- Declare the field static to share one broker across test methods; use a nonstatic field when each test requires a fresh container. section 14.1.5
- Feed input through KafkaProducer and validate output with KafkaConsumer, deleting test topics during teardown. 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
The listing shows the minimal annotated declaration and the Kafka image used by the integration test. section 14.1.5
