liveBook · Manning

KafkaContainer

As explained in Kafka Streams in Action, Second Edition

KafkaContainer is a Testcontainers component that provisions and manages a Kafka broker in a Docker container for tests.

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

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains annotations, image construction, lifecycle scope, bootstrap configuration, and surrounding test flow, but not Docker itself.
Examples
Uses a hopping-window Kafka Streams test with cp-kafka:7.5.1, timestamped records, expected counts, and topic cleanup.
What you can do after
Apply it via Testcontainers annotations to provision Kafka and wire integration-test clients to its dynamic bootstrap address.
How it connects
Connects KafkaContainer to Docker containers, Kafka Streams, KafkaProducer, KafkaConsumer, topics, and Testcontainers annotations.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kafka Streams in Action, Second Edition. The section links point to the parts of the book it draws on.