Kafka Containers

Testcontainers can be used to automatically instantiate and manage Apache Kafka containers. More precisely Testcontainers uses the official Docker images for Confluent OSS Platform

Benefits

  • Running a single node Kafka installation with just one line of code
  • No need to manage external Zookeeper installation, required by Kafka. But see below

Example

The following field in your JUnit test class will prepare a container running Kafka:

@Rule
public KafkaContainer kafka = new KafkaContainer();

Now your tests or any other process running on your machine can get access to running Kafka broker by using the following bootstrap server location:

kafka.getBootstrapServers()

Options

Selecting Kafka version

You can select a version of Confluent Platform by passing it to the container's constructor:

new KafkaContainer("4.1.2")
The correspondence between Confluent Platform versions and Kafka versions can be seen in Confluent documentation

Using external Zookeeper

If for some reason you want to use an externally running Zookeeper, then just pass its location during construction:

new KafkaContainer().withExternalZookeeper("localhost:2181")

Multi-container usage

If your test needs to run some other Docker container which needs access to the Kafka, do the following:

  • Run your other container on the same network as Kafka container, e.g.:
    new GenericContainer("myImage").withNetwork(kafka.getNetwork())
    
  • Use kafka.getNetworkAliases().get(0)+":9092" as bootstrap server location. Or just give your Kafka container a network alias of your liking.

Adding this module to your project dependencies

Add the following dependency to your pom.xml/build.gradle file:

testCompile "org.testcontainers:kafka:1.14.1"
<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>kafka</artifactId>
    <version>1.14.1</version>
    <scope>test</scope>
</dependency>