
How to Test Kafka Consumers With Testcontainers in Spring
Test a Spring Kafka consumer against a real broker in Testcontainers: send a record, wait for the effect, and cover retries and the dead letter topic.
Tag
223 posts

Test a Spring Kafka consumer against a real broker in Testcontainers: send a record, wait for the effect, and cover retries and the dead letter topic.

Compute Kafka consumer lag with the AdminClient, export it as a Micrometer gauge, set time based alerts and learn what a growing lag really means.

Understand how Kafka topics split into partitions, how the message key picks the partition and ordering follows from it, and size partitions for consumers.

Version event schemas with Avro and a schema registry in Java: define a schema, set compatibility modes, evolve fields safely, use Spring Kafka.

Write a Kafka consumer that survives restarts, duplicates, poison messages and slow batches, with manual commits, idempotency, retries and a dead letter topic

Add retries and a dead letter topic to a Spring Kafka consumer: DefaultErrorHandler, backoff, DeadLetterPublishingRecoverer and RetryableTopic.

Configure RabbitMQ with Spring AMQP so messages are never lost or stuck: acks, publisher confirms, bounded retries and dead letter queues.

Wire Testcontainers into Spring Boot tests with @ServiceConnection: no dynamic properties, supported containers, local development with TestApplication.

Mock HTTP dependencies with WireMock in JUnit: stubbing responses, verifying requests, simulating delays and faults, scenarios and Spring integration.

Make tests deterministic by injecting Clock, a Random seed and an id supplier, instead of calling now, Math.random and UUID.randomUUID inside the code.

Find and fix flaky Java tests: detect them with reruns and random order, learn the causes with examples and quarantine safely.

Migrate from java.util.Date, Calendar and Joda Time to java.time with a type table, boundary adapters, JPA and Jackson settings and time zone tests.