or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

apache-kafka-container.mdconfluent-kafka-container.mdindex.mdkafka-helper.mdlegacy-kafka-container.md
tile.json

tessl/maven-org-testcontainers--kafka

Testcontainers Kafka module for automatically instantiating and managing Apache Kafka containers in tests

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.testcontainers/kafka@1.21.x

To install, run

npx @tessl/cli install tessl/maven-org-testcontainers--kafka@1.21.0

index.mddocs/

Testcontainers Kafka

Testcontainers Kafka module provides container implementations for running Apache Kafka during integration testing. It supports multiple Kafka distributions including Apache Kafka, Confluent Platform, and legacy compatibility modes with embedded or external Zookeeper and modern KRaft mode for Zookeeper-free deployments.

Package Information

  • Package Name: org.testcontainers:kafka

  • Package Type: maven

  • Language: Java

  • Installation:

    Maven:

    <dependency>
        <groupId>org.testcontainers</groupId>
        <artifactId>kafka</artifactId>
        <version>1.21.3</version>
        <scope>test</scope>
    </dependency>

    Gradle:

    testImplementation 'org.testcontainers:kafka:1.21.3'

Core Imports

import org.testcontainers.kafka.KafkaContainer;
import org.testcontainers.kafka.ConfluentKafkaContainer;
import org.testcontainers.utility.DockerImageName;

For legacy support (deprecated):

import org.testcontainers.containers.KafkaContainer;

Basic Usage

import org.testcontainers.kafka.KafkaContainer;
import org.testcontainers.utility.DockerImageName;

// Apache Kafka using modern container
try (KafkaContainer kafka = new KafkaContainer("apache/kafka:3.8.0")) {
    kafka.start();
    String bootstrapServers = kafka.getBootstrapServers();
    
    // Use bootstrapServers to configure your Kafka clients
    // Example: "localhost:32768"
}

// Confluent Platform using modern container
try (ConfluentKafkaContainer kafka = new ConfluentKafkaContainer("confluentinc/cp-kafka:7.4.0")) {
    kafka.start();
    String bootstrapServers = kafka.getBootstrapServers();
}

Architecture

The Testcontainers Kafka module provides three main container implementations:

  • Modern Apache Kafka Container: org.testcontainers.kafka.KafkaContainer for apache/kafka and apache/kafka-native images with KRaft mode by default
  • Modern Confluent Container: org.testcontainers.kafka.ConfluentKafkaContainer for confluentinc/cp-kafka images (7.4.0+) with KRaft mode by default
  • Legacy Container: org.testcontainers.containers.KafkaContainer (deprecated) supporting embedded Zookeeper, external Zookeeper, and KRaft modes
  • Shared Helper: org.testcontainers.kafka.KafkaHelper providing common functionality and constants for modern containers

Capabilities

Apache Kafka Container

Modern container implementation for Apache Kafka with KRaft mode and flexible listener configuration.

public class KafkaContainer extends GenericContainer<KafkaContainer> {
    public KafkaContainer(String imageName);
    public KafkaContainer(DockerImageName dockerImageName);
    public KafkaContainer withListener(String listener);
    public KafkaContainer withListener(String listener, Supplier<String> advertisedListener);
    public String getBootstrapServers();
}

Apache Kafka Container

Confluent Kafka Container

Modern container implementation for Confluent Platform with KRaft mode and flexible listener configuration.

public class ConfluentKafkaContainer extends GenericContainer<ConfluentKafkaContainer> {
    public ConfluentKafkaContainer(String imageName);
    public ConfluentKafkaContainer(DockerImageName dockerImageName);
    public ConfluentKafkaContainer withListener(String listener);
    public ConfluentKafkaContainer withListener(String listener, Supplier<String> advertisedListener);
    public String getBootstrapServers();
}

Confluent Kafka Container

Legacy Kafka Container (Deprecated)

Legacy container implementation with Zookeeper and KRaft mode support. Use modern containers instead.

@Deprecated
public class KafkaContainer extends GenericContainer<KafkaContainer> {
    public KafkaContainer(DockerImageName dockerImageName);
    public KafkaContainer withEmbeddedZookeeper();
    public KafkaContainer withExternalZookeeper(String connectString);
    public KafkaContainer withKraft();
    public KafkaContainer withClusterId(String clusterId);
    public KafkaContainer withListener(Supplier<String> listenerSupplier);
    public String getBootstrapServers();
}

Legacy Kafka Container

Kafka Helper

Shared utility class providing common constants and functionality for modern Kafka containers.

class KafkaHelper {
    static final int KAFKA_PORT = 9092;
    static final String STARTER_SCRIPT = "/tmp/testcontainers_start.sh";
    static Map<String, String> envVars();
}

Kafka Helper

Types

Docker Image Names

// Apache Kafka images (for KafkaContainer)
DockerImageName.parse("apache/kafka:3.8.0")
DockerImageName.parse("apache/kafka-native:3.8.0")

// Confluent Platform images (for ConfluentKafkaContainer)  
DockerImageName.parse("confluentinc/cp-kafka:7.4.0")

// Legacy support (deprecated KafkaContainer)
DockerImageName.parse("confluentinc/cp-kafka:6.2.1")

Listener Configuration

// Simple listener format for modern containers
String listener = "kafka:19092";

// Supplier for dynamic advertised listeners
Supplier<String> advertisedListener = () -> "external-host:9092";

Common Container Methods

All Kafka containers inherit standard Testcontainers functionality:

// Container lifecycle
public void start();
public void stop();
public boolean isRunning();

// Network and port access
public String getHost();
public Integer getMappedPort(int originalPort);
public KafkaContainer withNetwork(Network network);
public KafkaContainer withNetworkAliases(String... aliases);

// Environment and configuration
public KafkaContainer withEnv(String key, String value);
public KafkaContainer withEnv(Map<String, String> env);

Constants

// Modern containers (KafkaContainer, ConfluentKafkaContainer)
static final int KAFKA_PORT = 9092;

// Legacy container
public static final int KAFKA_PORT = 9093;
public static final int ZOOKEEPER_PORT = 2181;
public static final String DEFAULT_CLUSTER_ID = "4L6g3nShT-eMCtK--X86sw";