KafkaProducerUseIteratorFalseIT.java
Overview
`KafkaProducerUseIteratorFalseIT` is an integration test class for the Apache Camel Kafka component. It verifies the behavior of the Kafka producer when configured with the `useIterator=false` option. This option controls how Camel sends a collection of messages to Kafka — whether as individual messages (iterator=true) or as a single message (iterator=false).
Specifically, the test ensures that when `useIterator=false`, the entire collection is sent as one Kafka message rather than multiple messages. The test sends a list of strings as a single message to a Kafka topic and verifies that only one Kafka record is produced and consumed.
This class is part of the Kafka integration tests and extends `BaseKafkaTestSupport`, which provides Kafka environment setup and teardown capabilities for testing.
Class Summary
KafkaProducerUseIteratorFalseIT
Visibility | Type | Description |
|---|---|---|
public | class | Integration test for Kafka producer with `useIterator=false` configuration. |
Detailed Description of Members
Constants
Name | Type | Description |
|---|---|---|
`TOPIC` | String | Kafka topic name used for testing (`"use-iterator-false"`). |
`FROM_URI` | String | Kafka consumer endpoint URI with specific consumer options and interceptor configured for testing. |
Lifecycle Methods
@BeforeEach void init()
Purpose: Clears the static records captured by the
MockConsumerInterceptorbefore each test to ensure a clean test state.Parameters: None
Returns: void
@AfterEach void after()
Purpose: Deletes the test Kafka topic after each test to clean up resources.
Parameters: None
Returns: void
Test Methods
@Test void testUseIteratorFalse() throws Exception
Purpose: Tests that sending a collection of messages with
useIterator=falseresults in a single Kafka record.Parameters: None
Returns: void
Behavior:
Creates a
List<String>with two elements:"first","second".Sends this list as a single message to the Kafka topic via the Camel route.
Expects the mock endpoint (
mock:result) to receive the entire list serialized as a string.Asserts that exactly one Kafka record was captured by the
MockConsumerInterceptor.
Usage Example (implicit in test):
List<String> body = new ArrayList<>(); body.add("first"); body.add("second"); contextExtension.getProducerTemplate().sendBody("direct:start", body);
Overridden Methods
protected RouteBuilder createRouteBuilder()
Purpose: Defines Camel routes for the test.
Returns:
RouteBuilderinstance with two routes configured:Producer Route:
From:
"direct:start"endpointTo: Kafka topic with
useIterator=falseset ("kafka:use-iterator-false?groupId=KafkaProducerUseIteratorFalseIT&useIterator=false")Effect: Sends the entire collection as a single Kafka message.
Consumer Route:
From: Kafka consumer endpoint defined by
FROM_URITo:
"mock:result"endpointEffect: Consumes messages from Kafka for assertions.
Usage example snippet in context:
from("direct:start").to("kafka:" + TOPIC + "?groupId=KafkaProducerUseIteratorFalseIT&useIterator=false"); from(FROM_URI).to("mock:result");
Important Implementation Details
useIterator=false: This Kafka producer option tells Camel to send the entire collection as a single message instead of iterating through the collection and sending multiple messages. This is the core feature under test.
MockConsumerInterceptor: Used to capture Kafka records consumed by the test, enabling verification of the exact number of messages received.
Kafka Topic Cleanup: Topics are explicitly deleted after each test to avoid side effects in subsequent tests.
Assertions:
The received message body matches the string representation of the original list.
Exactly one Kafka record is produced and consumed, confirming that the collection was sent as a single message.
Interaction with Other Components
BaseKafkaTestSupport: This class extends
BaseKafkaTestSupport, which sets up the Kafka test environment, including embedded Kafka brokers and admin clients.MockConsumerInterceptor: Intercepts Kafka consumer records for validation.
Apache Camel Context: Utilizes Camel's routing and testing framework (
contextExtension,MockEndpoint,ProducerTemplate) to send and receive messages.Kafka Broker: The test sends and consumes messages to/from a real Kafka topic named
"use-iterator-false".
Visual Diagram
classDiagram
class KafkaProducerUseIteratorFalseIT {
- static final String TOPIC
- static final String FROM_URI
+ void init()
+ void after()
+ void testUseIteratorFalse()
+ RouteBuilder createRouteBuilder()
}
class BaseKafkaTestSupport
KafkaProducerUseIteratorFalseIT --|> BaseKafkaTestSupport
class RouteBuilder {
+void configure()
}
KafkaProducerUseIteratorFalseIT o-- RouteBuilder : creates
class MockConsumerInterceptor {
+static List recordsCaptured
}
KafkaProducerUseIteratorFalseIT ..> MockConsumerInterceptor : uses for record capture
class MockEndpoint {
+void expectedBodiesReceived(Object)
+void assertIsSatisfied(long)
}
KafkaProducerUseIteratorFalseIT ..> MockEndpoint : asserts results
Summary
`KafkaProducerUseIteratorFalseIT` is an integration test validating that when Apache Camel's Kafka producer is configured with `useIterator=false`, a collection of messages is sent as a single Kafka message. It leverages Camel routes, a mock Kafka consumer interceptor, and JUnit 5 lifecycle methods to ensure reliable and isolated testing of this behavior within the Kafka component integration suite.