A broker-backed test checks protocol and client behavior beyond what a mock can show. Testcontainers launches a real service in a container; embedded broker libraries run a broker within the test process. Neither automatically recreates a production cluster’s network, security, or failure conditions.
A bounded Kafka round trip
The example below targets Java 17, Maven 3.9, Testcontainers 1.20.4, Kafka clients 3.8.1, and JUnit 5.11.3. It requires a working Docker-compatible runtime. The editorial environment did not have a running Docker daemon, so broker execution remains a separate integration check.
A minimal pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>example</groupId><artifactId>kafka-test</artifactId><version>1.0</version>
<properties><maven.compiler.release>17</maven.compiler.release></properties>
<dependencies>
<dependency><groupId>org.testcontainers</groupId><artifactId>kafka</artifactId><version>1.20.4</version><scope>test</scope></dependency>
<dependency><groupId>org.apache.kafka</groupId><artifactId>kafka-clients</artifactId><version>3.8.1</version><scope>test</scope></dependency>
<dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>5.11.3</version><scope>test</scope></dependency>
</dependencies>
<build><plugins>
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.13.0</version></plugin>
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.5.2</version></plugin>
</plugins></build>
</project>
Save this as src/test/java/KafkaRoundTripTest.java:
import java.time.Duration;
import java.util.List;
import java.util.Properties;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.junit.jupiter.api.Test;
import org.testcontainers.kafka.KafkaContainer;
import static org.junit.jupiter.api.Assertions.assertTrue;
class KafkaRoundTripTest {
@Test
void roundTrip() throws Exception {
try (var kafka = new KafkaContainer("apache/kafka-native:3.8.0")) {
kafka.start();
String topic = "test-" + UUID.randomUUID();
Properties base = new Properties();
base.put("bootstrap.servers", kafka.getBootstrapServers());
try (var admin = AdminClient.create(base)) {
admin.createTopics(List.of(new NewTopic(topic, 1, (short) 1)))
.all().get(30, TimeUnit.SECONDS);
}
Properties producerConfig = new Properties();
producerConfig.putAll(base);
producerConfig.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
producerConfig.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
try (var producer = new KafkaProducer<String, String>(producerConfig)) {
producer.send(new ProducerRecord<>(topic, "key", "value"))
.get(30, TimeUnit.SECONDS);
}
Properties consumerConfig = new Properties();
consumerConfig.putAll(base);
consumerConfig.put("group.id", UUID.randomUUID().toString());
consumerConfig.put("auto.offset.reset", "earliest");
consumerConfig.put("enable.auto.commit", "false");
consumerConfig.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
consumerConfig.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
try (var consumer = new KafkaConsumer<String, String>(consumerConfig)) {
consumer.subscribe(List.of(topic));
long deadline = System.nanoTime() + Duration.ofSeconds(30).toNanos();
boolean found = false;
while (!found && System.nanoTime() < deadline) {
for (var record : consumer.poll(Duration.ofMillis(250))) {
if ("key".equals(record.key()) && "value".equals(record.value())) {
found = true;
}
}
}
assertTrue(found, "Expected the produced record before the deadline");
}
}
}
}
Run mvn test. A first poll may return no records while subscription/assignment completes, so the test polls until a deadline. ConsumerRecords is iterable; use its iterator rather than assuming a .stream() method exists. All clients and the container have explicit lifecycle cleanup.
Conceptual illustration; labels describe responsibilities, not a deployment specification.
Embedded does not mean mocked
Spring Kafka’s EmbeddedKafka, the embedded-kafka project, and kafka-unit are separate libraries with different APIs and supported Kafka versions. Choose one explicitly. An embedded broker can use real Kafka code and disk/network resources; it is not necessarily an in-memory simulation.
| Need | Test design |
|---|---|
| Serialization and application logic | Small local tests with explicit inputs |
| Client/broker integration | Pinned broker-backed round trip |
| Transactions or security | Configure those features and test their actual behavior |
| Failover and partitions | A deliberate multi-node/network failure exercise |
Keep database, browser, and mock-server tests separate from this broker exercise so each test states the service behavior it actually verifies.