Class Kafka
- All Implemented Interfaces:
KafkaAsserts,KafkaConfig,KafkaInt
It is recommended to use a single instance:
Kafka kafka = Kafka.getInstance();
Uses a static consumer membership to reduce consumer rebalancing delays after reconnects.
Important: Consumed messages remain available after the test execution. It is recommended to purge messages between tests or use unique test data.
Consumed messages are limited by default to KafkaConsumerHelper.maxConsumedMessages
(only the latest messages are grabbed). The limit can be changed using
setMaxConsumeMessages(int) or configuration:
modules.kafka.max-consumed-messages.
Consumed messages are returned in reverse order (newest messages first) by default to improve performance for fast tests.
Assertions use the default await timeout
KafkaConsumerHelper.awaitMs and wait for the first message.
The timeout can be changed using setAwaitMs(int) or configuration:
modules.kafka.await.
For testing, it is recommended to use one partition per topic, but multiple partitions are supported.
By default, the consumer uses a static group ID.
A unique consumer can be enabled using setUniqueConsumer(boolean)
or configuration:
modules.kafka.generate-unique-consumer.
- Since:
- 1.0.0
-
Field Summary
Fields inherited from class net.bugreaper.modules.kafka.setup.KafkaConsumerHelper
adminClient, awaitMs, bootStrapServer, consumerTimeoutMs, maxConsumedMessages, reverseMessages, uniqueConsumerGroup -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidcreateTopic(String topic) Creates a topic with one partition.voidcreateTopic(String topic, int partitionsCount) Creates a topic with multiple partitions.voiddeleteTopic(String topic) Deletes a topic.net.bugreaper.core.assertable.AssertableStringListReturns a list of all topic names.Returns and logs (at INFO level) a human-readable summary of all resolved configuration values.static KafkaintgetMessagesCountInTopic(String topic) Returns the number of messages in a topic.intgetPartitionsCount(String topic) Returns the number of partitions in a topic.net.bugreaper.core.assertable.AssertableStringListgrabMessagesFromTopic(String topic) Grabs messages from a topic using a consumer without committing offsets.net.bugreaper.core.assertable.AssertableStringListgrabMessagesFromTopic(String topic, String key) Grabs messages from a topic using a consumer without committing offsets.voidpurgeTopic(String topic) Purges a topic by removing messages from all partitions.voidseeMessagesContainJson(String topic, String expectedJsonPart) Asserts that at least one of messages in topic contains the specified JSON without strict array ordering.voidseeMessagesContainText(String topic, String expectedPart) Asserts that at least one of messages in topic contains the specified text.voidseeMessagesCountInTopicExactly(String topic, int expectedCount) Asserts that the number of messages in the topic is exactly the expected count.voidseeMessagesHaveEqualJson(String topic, String expectedJson) Asserts that at least one of messages in topic is equal to the specified JSON with strict array ordering.voidseeMessagesHaveEqualText(String topic, String expectedText) Asserts that at least one message in the topic is equal to the expected text.voidseeTopicExists(String topic) Asserts that the topic exists.voidseeTopicIsEmpty(String topic) Asserts that the topic is empty.voidseeTopicIsNotEmpty(String topic) Asserts that the topic is not emptyvoidsendToTopic(String topic, String message) Sends a message to a topic.voidsendToTopicWithKey(String topic, String key, String message) Sends a message with a key to a topic.setAwaitMs(int awaitMs) Configures the global await timeout for assertions and operations that use await.setConsumerTimeoutMs(int consumerTimeoutMs) Sets the consumer timeout.setMaxConsumeMessages(int maxMessages) Sets the maximum number of latest messages to consume.setReverseMessages(boolean reverseMessages) Sets the message ordering behavior.setUniqueConsumer(boolean unique) Enables or disables generating a unique consumer group ID.Methods inherited from class net.bugreaper.modules.kafka.setup.KafkaConsumerHelper
assertCountInTopicMethod, consumer, getAllTopicsNamesMethod, getTopicMessageCountMethod, grabMessagesFromTopicMethod, seeTopicIsEmptyMethod, seeTopicIsNotEmptyMethod
-
Constructor Details
-
Kafka
Creates a Kafka helper with the specified bootstrap server.- Parameters:
bootStrapServer- kafka server (example:"my-kafka:9092")
-
Kafka
public Kafka()Constructs a MongoDb client using YAML configuration.Loads configuration values from a YAML file.
Default file:
bugreaper.ymlCustom file: using
-DbugreaperEnv=testloadsbugreaper-test.ymlmodules: kafka: url: localhost:9096 await: 300 # (optional) max-consumed-messages: 5 # (optional) max-consumer-timeout: 700 # (optional) generate-unique-consumer: true # (optional) reverse-messages: true # (optional)Missing required keys will result in configuration errors. Missing optional keys will fall back to predefined defaults.
- Throws:
IllegalArgumentException- if the configuration contains invalid values
-
-
Method Details
-
getInstance
Returns the instance ofKafkawith config builderKafka().This implementation is thread-safe using method-level synchronization.
- Returns:
- the shared instance of
Kafka - Throws:
IllegalArgumentException- if the configuration contains invalid values- See Also:
-
setAwaitMs
Description copied from interface:KafkaConfigConfigures the global await timeout for assertions and operations that use await.- Specified by:
setAwaitMsin interfaceKafkaConfig- Parameters:
awaitMs- await timeout in milliseconds- Returns:
- this instance for method chaining
-
setMaxConsumeMessages
Description copied from interface:KafkaConfigSets the maximum number of latest messages to consume.- Specified by:
setMaxConsumeMessagesin interfaceKafkaConfig- Parameters:
maxMessages- maximum number of messages to consume from the latest offset- Returns:
- this instance for method chaining
-
setConsumerTimeoutMs
Description copied from interface:KafkaConfigSets the consumer timeout.- Specified by:
setConsumerTimeoutMsin interfaceKafkaConfig- Parameters:
consumerTimeoutMs- maximum consumer execution time in milliseconds before interruption- Returns:
- this instance for method chaining
-
setUniqueConsumer
Description copied from interface:KafkaConfigEnables or disables generating a unique consumer group ID.- Specified by:
setUniqueConsumerin interfaceKafkaConfig- Parameters:
unique-trueto generate a unique consumer group ID,falseto use the default group ID- Returns:
- this instance for method chaining
-
setReverseMessages
Description copied from interface:KafkaConfigSets the message ordering behavior.Controls whether consumed messages are returned in reverse order (newest messages first).
- Specified by:
setReverseMessagesin interfaceKafkaConfig- Parameters:
reverseMessages-trueto return messages in reverse order,falseto keep the original order- Returns:
- this instance for method chaining
-
getConfigSummary
Description copied from interface:KafkaConfigReturns and logs (at INFO level) a human-readable summary of all resolved configuration values.The summary includes values loaded from the YAML configuration file as well as any fields overridden programmatically after construction. Optional fields that were not present in the configuration and resolved via default values may also be included.
- Specified by:
getConfigSummaryin interfaceKafkaConfig- Returns:
- String with summary
-
createTopic
Description copied from interface:KafkaIntCreates a topic with one partition.- Specified by:
createTopicin interfaceKafkaInt- Parameters:
topic- topic name
-
createTopic
@Step("(Kafka) Create topic: \'{topic}\' with <{partitionsCount}> partitions") public void createTopic(String topic, int partitionsCount) Description copied from interface:KafkaIntCreates a topic with multiple partitions.- Specified by:
createTopicin interfaceKafkaInt- Parameters:
topic- topic namepartitionsCount- number of partitions
-
deleteTopic
Description copied from interface:KafkaIntDeletes a topic.- Specified by:
deleteTopicin interfaceKafkaInt- Parameters:
topic- topic name
-
purgeTopic
Description copied from interface:KafkaIntPurges a topic by removing messages from all partitions.- Specified by:
purgeTopicin interfaceKafkaInt- Parameters:
topic- topic name
-
sendToTopic
@Step("(Kafka) Send message to topic: \'{topic}\'") public void sendToTopic(String topic, String message) Description copied from interface:KafkaIntSends a message to a topic.There may be a delay before the consumer can see the message, therefore await is used when consuming messages.
- Specified by:
sendToTopicin interfaceKafkaInt- Parameters:
topic- topic namemessage- message content
-
sendToTopicWithKey
@Step("(Kafka) Send message to topic: \'{topic}\' with key \'{key}\'") public void sendToTopicWithKey(String topic, String key, String message) Description copied from interface:KafkaIntSends a message with a key to a topic.There may be a delay before the consumer can see the message
- Specified by:
sendToTopicWithKeyin interfaceKafkaInt- Parameters:
topic- topic namekey- message key included in the Kafka recordmessage- message content
-
grabMessagesFromTopic
@Step("(Kafka) Grab messages from topic: \'{topic}\'") public net.bugreaper.core.assertable.AssertableStringList grabMessagesFromTopic(String topic) Description copied from interface:KafkaIntGrabs messages from a topic using a consumer without committing offsets.Uses await until at least one message exists in the topic.
The maximum number of messages is configured globally (only the latest messages are grabbed).
Messages are returned in reverse order (newest messages first) to optimize processing when working with large amounts of data.
Uses static consumer membership to reduce consumer rebalancing delays after reconnecting.
Example:
grabMessagesFromTopic("test_queue").seeListAnyEquals("my message")- Specified by:
grabMessagesFromTopicin interfaceKafkaInt- Parameters:
topic- topic name- Returns:
AssertableStringList
-
grabMessagesFromTopic
@Step("(Kafka) Grab messages from topic: \'{topic}\' with key: \'{key}\'") public net.bugreaper.core.assertable.AssertableStringList grabMessagesFromTopic(String topic, String key) Description copied from interface:KafkaIntGrabs messages from a topic using a consumer without committing offsets.Uses await until at least one message exists in the topic.
The maximum number of messages is configured globally (only the latest messages are grabbed).
Messages are returned in reverse order (newest messages first) to optimize processing when working with large amounts of data.
Uses static consumer membership to reduce consumer rebalancing delays after reconnecting.
Example:
grabMessagesFromTopic("test_topic", "my-key").seeListAnyEquals("my message")- Specified by:
grabMessagesFromTopicin interfaceKafkaInt- Parameters:
topic- topic namekey- message key to filter consumed records- Returns:
AssertableStringList
-
getAllTopicsNames
@Step("(Kafka) Get all topics names") public net.bugreaper.core.assertable.AssertableStringList getAllTopicsNames()Description copied from interface:KafkaIntReturns a list of all topic names.Example:
getAllTopicsNames().seeListAnyContains("test_topic")- Specified by:
getAllTopicsNamesin interfaceKafkaInt- Returns:
AssertableStringList
-
getMessagesCountInTopic
Description copied from interface:KafkaIntReturns the number of messages in a topic.- Specified by:
getMessagesCountInTopicin interfaceKafkaInt- Parameters:
topic- topic name- Returns:
- number of messages in the topic
-
getPartitionsCount
Description copied from interface:KafkaIntReturns the number of partitions in a topic.- Specified by:
getPartitionsCountin interfaceKafkaInt- Parameters:
topic- topic name- Returns:
- number of partitions in the topic
-
seeMessagesHaveEqualText
@Step("(Kafka)[ASSERT] Topic: \'{topic}\' has a message EQUALS expected text") public void seeMessagesHaveEqualText(String topic, String expectedText) Description copied from interface:KafkaAssertsAsserts that at least one message in the topic is equal to the expected text.Uses await until at least one message exists.
- Specified by:
seeMessagesHaveEqualTextin interfaceKafkaAsserts- Parameters:
topic- topic nameexpectedText- expected message text
-
seeMessagesContainText
@Step("(Kafka)[ASSERT] Topic: \'{topic}\' has a message CONTAINS expected text") public void seeMessagesContainText(String topic, String expectedPart) Description copied from interface:KafkaAssertsAsserts that at least one of messages in topic contains the specified text.Uses await until at least one message exists.
- Specified by:
seeMessagesContainTextin interfaceKafkaAsserts- Parameters:
topic- topic nameexpectedPart- expected text part
-
seeMessagesHaveEqualJson
@Step("(Kafka)[ASSERT] Topic: \'{topic}\' has a message EQUALS expected JSON") public void seeMessagesHaveEqualJson(String topic, String expectedJson) Description copied from interface:KafkaAssertsAsserts that at least one of messages in topic is equal to the specified JSON with strict array ordering.Uses await until at least one message exists.
- Specified by:
seeMessagesHaveEqualJsonin interfaceKafkaAsserts- Parameters:
topic- topic nameexpectedJson- expected full JSON with strict array ordering
-
seeMessagesContainJson
@Step("(Kafka)[ASSERT] Topic: \'{topic}\' has a message CONTAINS expected JSON") public void seeMessagesContainJson(String topic, String expectedJsonPart) Description copied from interface:KafkaAssertsAsserts that at least one of messages in topic contains the specified JSON without strict array ordering.Uses await until at least one message exists.
- Specified by:
seeMessagesContainJsonin interfaceKafkaAsserts- Parameters:
topic- topic nameexpectedJsonPart- expected JSON subset (array order is ignored, but the number of elements must match if provided)
-
seeMessagesCountInTopicExactly
@Step("(Kafka)[ASSERT] Topic: \'{topic}\' contains EXACTLY <{expectedCount}> messages") public void seeMessagesCountInTopicExactly(String topic, int expectedCount) Description copied from interface:KafkaAssertsAsserts that the number of messages in the topic is exactly the expected count.Uses await.
- Specified by:
seeMessagesCountInTopicExactlyin interfaceKafkaAsserts- Parameters:
topic- topic nameexpectedCount- expected number of messages
-
seeTopicIsNotEmpty
@Step("(Kafka)[ASSERT] Topic: \'{topic}\' is not empty") public void seeTopicIsNotEmpty(String topic) Description copied from interface:KafkaAssertsAsserts that the topic is not emptyUses await.
- Specified by:
seeTopicIsNotEmptyin interfaceKafkaAsserts- Parameters:
topic- topic name
-
seeTopicIsEmpty
Description copied from interface:KafkaAssertsAsserts that the topic is empty.Uses await.
- Specified by:
seeTopicIsEmptyin interfaceKafkaAsserts- Parameters:
topic- topic name
-
seeTopicExists
Description copied from interface:KafkaAssertsAsserts that the topic exists.Uses await.
- Specified by:
seeTopicExistsin interfaceKafkaAsserts- Parameters:
topic- topic name
-