Kafka¶
Steps for interacting with Apache Kafka message broker
Multi-line Content
Steps ending with : accept multi-line content using Gherkin's docstring syntax ("""). See examples below each section.
Topic Management¶
| Step | Description |
|---|---|
"{resource}" topic "events" exists |
Asserts a Kafka topic exists |
"{resource}" creates topic "events" |
Creates a Kafka topic with 1 partition |
"{resource}" creates topic "events" with "3" partitions |
Creates a Kafka topic with specified partitions |
Publishing¶
| Step | Description |
|---|---|
"{resource}" message header "trace-id" is "abc-123" |
Sets a header on the next message published (any publish step) |
"{resource}" publishes to "events": |
Publishes a message to a topic |
"{resource}" publishes to "events" with key "user-123": |
Publishes a message with a key to a topic |
"{resource}" publishes json to "events": |
Publishes a JSON message to a topic |
"{resource}" publishes json to "events" with key "user-123": |
Publishes a JSON message with a key |
"{resource}" publishes messages to "events": |
Publishes multiple messages from a table |
Examples¶
Publishes a message to a topic:
Publishes a message with a key to a topic:
Publishes a JSON message to a topic:
Publishes a JSON message with a key:
Publishes multiple messages from a table:
Avro and Schema Registry¶
| Step | Description |
|---|---|
"{resource}" registers schema for subject "orders-value": |
Registers an Avro schema under a subject |
"{resource}" registers schema for subject "orders-value" from file "schemas/order.avsc" |
Registers an Avro schema (.avsc) from a file |
"{resource}" publishes avro to "orders": |
Publishes JSON as Avro, using the latest schema of the topic's value subject |
"{resource}" publishes avro to "orders" with key "order-1": |
Publishes JSON as Avro with a string key |
"{resource}" receives avro from "orders" within "10s": |
Waits for an Avro message whose JSON form contains the given fields |
"{resource}" last message avro matches: |
Asserts the last message, decoded from Avro, equals the JSON exactly |
"{resource}" last message avro contains: |
Asserts the last message, decoded from Avro, contains the JSON fields |
Examples¶
Registers an Avro schema under a subject:
"{resource}" registers schema for subject "orders-value":
"""
{"type": "record", "name": "Order", "fields": [{"name": "id", "type": "string"}]}
"""
Publishes JSON as Avro, using the latest schema of the topic's value subject:
Publishes JSON as Avro with a string key:
Waits for an Avro message whose JSON form contains the given fields:
Asserts the last message, decoded from Avro, equals the JSON exactly:
Asserts the last message, decoded from Avro, contains the JSON fields:
Consuming¶
| Step | Description |
|---|---|
"{resource}" consumes from "events" |
Starts consuming messages from a topic |
"{resource}" receives from "events" within "5s" |
Waits for a message from a topic within timeout |
"{resource}" receives from "events" within "5s": |
Asserts a specific message is received within timeout |
"{resource}" receives from "events" with key "user-123" within "5s" |
Asserts a message with specific key is received |
Examples¶
Asserts a specific message is received within timeout:
Assertions¶
| Step | Description |
|---|---|
"{resource}" topic "events" has "3" messages |
Asserts topic has exactly N messages consumed |
"{resource}" topic "events" is empty |
Asserts no messages have been consumed from topic |
"{resource}" last message contains: |
Asserts the last consumed message contains content |
"{resource}" last message has key "user-123" |
Asserts the last consumed message has specific key |
"{resource}" last message has header "content-type" with value "application/json" |
Asserts the last message has a header with value |
"{resource}" receives messages from "events" in order: |
Asserts messages are received in specified order |
Examples¶
Asserts the last consumed message contains content:
Asserts messages are received in specified order:
Consumer Groups¶
| Step | Description |
|---|---|
"{resource}" consumes from "events" as consumer group "billing" |
Joins a consumer group on a topic; its messages are received like any other |
"{resource}" consumer group "billing" is consuming "events" within "30s" |
Waits until a member of the consumer group is assigned the topic, as the broker sees it |
"{resource}" consumer group "billing" has no members |
Asserts the consumer group has no members, as the broker sees it |