liveBook · Manning

TestOutputTopic

As explained in Kafka Streams in Action, Second Edition

TestOutputTopic is a Kafka Streams test utility representing a topology output topic and providing methods for reading produced records.

TestOutputTopic is a Kafka Streams test utility that represents a topology's output topic. Create it through TopologyTestDriver with the exact topic name used by the topology, plus key and value deserializers that convert serialized output back into objects for assertions. section 14.1.3 section 14.2

Listing 14.11 Building the TopologyTestDriver and input and output topics
try (TopologyTestDriver driver =
                 new TopologyTestDriver(yellingTopology)) {     #1

    TestInputTopic<String, String> inputTopic =      #2
            driver.createInputTopic("src-topic",
                                    stringSerializer,
                                    stringSerializer);

    TestOutputTopic<String, String> outputTopic =     #3
            driver.createOutputTopic("out-topic",
                                    stringDeserializer,
                                    stringDeserializer);

Use the setup listing to see TestOutputTopic paired with TestInputTopic inside a TopologyTestDriver. section 14.1.3

After records pass through TestInputTopic, read the produced results from TestOutputTopic and compare them with expected values. Choose a reader according to the assertion: readValue or readValuesToList for values, readKeyValue or readKeyValuesToList for ordered key-value records, readRecord or readRecordsToList when timestamps or headers matter, and readKeyValuesToMap for the latest result per key. section 14.1.3 section 14.1.4

Validate a topology's output

Send values through the input topic, derive the expected uppercase values, read the output list, and assert equality. The output topic captures the topology's serialized results as deserialized Java values. section 14.1.3 section 14.1.3

Listing 14.12 Sending records through the topology and validating results
List<String> inputValues = List.of("if you don't eat your meat",    #1
                    "you can't have any pudding!",
                    "How can you have any pudding",
                    "if you don't eat your meat!" );

inputTopic.pipeValueList(inputValues);                   #2
List<String> expectedOutput = inputValues.stream()
                                        .map(String::toUpperCase)
                                        .toList();         #3

List<String> actualOutput = outputTopic.readValuesToList();    #4

assertThat(actualOutput, equalTo(expectedOutput));    #5

Use the validation listing for a complete pipe, expected-output, read, and assertion sequence. section 14.1.3

For stateful or time-driven topologies, TestOutputTopic also exposes intermediate or final behavior: read ordered updates, collect the latest value for each key, inspect queue size, or read results after timestamps advance and suppressed windowed output is emitted. section 14.1.4 section 14.1.4

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains construction, deserialization, output-reading methods, ordered assertions, final-per-key views, and time-driven output checks.
Examples
Demonstrates uppercase output, reduce results, latest values per key, punctuation, and suppressed windowed results.
What it assumes
Assumes a Kafka Streams topology and TopologyTestDriver while demonstrating input topics, serializers, deserializers, and assertions.
What you can do after
Diagnose and validate topology behavior by reading produced records and comparing ordered, aggregated, timestamped, or windowed results.
How it connects
Connects TestInputTopic, TopologyTestDriver, deserializers, state stores, stream time, and windowed results.

Also explained in

Compare how 2 books explain TestOutputTopic →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kafka Streams in Action, Second Edition. The section links point to the parts of the book it draws on.