Kafka Module
Testcontainers can be used to automatically instantiate and manage Apache Kafka containers.
Currently, two different Kafka images are supported:
org.testcontainers.kafka.ConfluentKafkaContainersupports confluentinc/cp-kafkaorg.testcontainers.kafka.KafkaContainersupports apache/kafka and apache/kafka-native
Note
org.testcontainers.containers.KafkaContaineris deprecated. Please useorg.testcontainers.kafka.ConfluentKafkaContainerororg.testcontainers.kafka.KafkaContainerinstead, depending on the used image.
Benefits
- Running a single node Kafka installation with just one line of code
- No need to manage external Zookeeper installation, required by Kafka.
Example
Using org.testcontainers.kafka.KafkaContainer
Create a KafkaContainer to use it in your tests:
Creating a KafkaContainer inside_block:constructorWithVersion
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:
Bootstrap Servers inside_block:getBootstrapServers
Using org.testcontainers.kafka.ConfluentKafkaContainer
Note Compatible with
confluentinc/cp-kafkaimages version7.4.0and later.
Create a ConfluentKafkaContainer to use it in your tests:
Creating a ConfluentKafkaContainer inside_block:constructorWithVersion
Options
Using Kraft mode
Note Only available for
org.testcontainers.containers.KafkaContainer
KRaft mode was declared production ready in 3.3.1 (confluentinc/cp-kafka:7.3.x)
Kraft mode inside_block:withKraftMode
See the versions interoperability matrix for more details.
Register listeners
There are scenarios where additional listeners are needed because the consumer/producer can be in another container in the same network or a different process where the port to connect differs from the default exposed port. E.g Toxiproxy.
Register additional listener inside_block:registerListener
Container defined in the same network:
Create kcat container inside_block:createKCatContainer
Client using the new registered listener:
Produce/Consume via new listener inside_block:produceConsumeMessage
Adding this module to your project dependencies
Add the following dependency to your pom.xml/build.gradle file:
Gradle
testImplementation "org.testcontainers:testcontainers-kafka:{{latest_version}}"Maven
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-kafka</artifactId>
<version>{{latest_version}}</version>
<scope>test</scope>
</dependency>