As a collection
Extraction assertions
Make a topic feel like a collection. Useful when you want to assert over every published key, every payload value, or every extracted JsonPath value.
Start with KafkaAssertions.assertThatTopic(topic) and then switch into an extracted-values assertion chain.
Extraction entry points
| Property | Default | Description |
|---|---|---|
extractingKeys() | — | preserves topic read order |
extractingValues() | — | raw string values |
extractingValues(Class<V>) | — | JSON DTOs, Avro or Protobuf types — uses type auto-detection |
extractingJsonPath(String) | — | evaluates the JsonPath against each string payload |
extractingJsonPath(Class<V>, String) | — | deserializes first, then evaluates the JsonPath |
All extraction methods return KafkaValuesAssert, which exposes normal AssertJ list assertions like containsExactly(...), containsExactlyInAnyOrder(...), containsOnly(...), hasSize(...), allSatisfy(...), anySatisfy(...), and noneSatisfy(...).
Keys
Java
assertThatTopic("orders-topic")
.extractingKeys()
.containsExactly("order-1", "order-2");Raw string payloads
Java
assertThatTopic("orders-topic")
.extractingJsonPath("$.product")
.containsExactly("Widget A", "Widget B");Java
assertThatTopic("orders-topic")
.extractingValues()
.containsOnly(
"{\"status\":\"CREATED\"}",
"{\"status\":\"FAILED\"}");Typed JSON payloads
Java
assertThatTopic("orders-topic")
.extractingJsonPath(OrderEvent.class, "$.product")
.containsExactlyInAnyOrder("Widget A", "Widget B");Java
assertThatTopic("orders-topic")
.extractingValues(OrderEvent.class)
.allSatisfy(order -> assertThat(order.getQuantity()).isPositive())
.anySatisfy(order -> assertThat(order.getProduct()).isEqualTo("Widget A"));Typed Avro payloads
Avro GenericRecord
Java
assertThatTopic("trades-topic")
.extractingJsonPath(GenericRecord.class, "$.message")
.containsExactly("Trade created", "Trade booked");Avro SpecificRecord
Java
assertThatTopic("trades-topic")
.extractingJsonPath(TradeEvent.class, "$.symbol")
.containsExactly("AAPL", "MSFT");Failure behavior
JsonPath extraction fails fast when:
- a payload is not valid JSON for the requested extraction
- a record does not contain the requested path
The failure message includes the topic name, the JsonPath, and the failing record index so it's obvious which record broke the expectation.