docs/modules/kafka.md
Testcontainers can be used to automatically instantiate and manage Apache Kafka containers.
Currently, two different Kafka images are supported:
org.testcontainers.kafka.ConfluentKafkaContainer supports
confluentinc/cp-kafkaorg.testcontainers.kafka.KafkaContainer supports apache/kafka and apache/kafka-native!!! note
org.testcontainers.containers.KafkaContainer is deprecated.
Please use org.testcontainers.kafka.ConfluentKafkaContainer or org.testcontainers.kafka.KafkaContainer instead, depending on the used image.
Create a KafkaContainer to use it in your tests:
Creating a KafkaContainer inside_block:constructorWithVersion
<!--/codeinclude-->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:
<!--codeinclude-->Bootstrap Servers inside_block:getBootstrapServers
<!--/codeinclude-->!!! note
Compatible with confluentinc/cp-kafka images version 7.4.0 and later.
Create a ConfluentKafkaContainer to use it in your tests:
Creating a ConfluentKafkaContainer inside_block:constructorWithVersion
<!--/codeinclude-->!!! note
Only available for org.testcontainers.containers.KafkaContainer
KRaft mode was declared production ready in 3.3.1 (confluentinc/cp-kafka:7.3.x)
<!--codeinclude-->Kraft mode inside_block:withKraftMode
<!--/codeinclude-->See the versions interoperability matrix for more details.
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.
<!--codeinclude-->Register additional listener inside_block:registerListener
<!--/codeinclude-->Container defined in the same network:
<!--codeinclude-->Create kcat container inside_block:createKCatContainer
<!--/codeinclude-->Client using the new registered listener:
<!--codeinclude-->Produce/Consume via new listener inside_block:produceConsumeMessage
<!--/codeinclude-->Add the following dependency to your pom.xml/build.gradle file:
=== "Gradle"
groovy testImplementation "org.testcontainers:testcontainers-kafka:{{latest_version}}"
=== "Maven"
xml <dependency> <groupId>org.testcontainers</groupId> <artifactId>testcontainers-kafka</artifactId> <version>{{latest_version}}</version> <scope>test</scope> </dependency>