KeyValueStore is the local state-store interface used by Kafka Streams applications to retain keyed results. In a stateful topology, it lets tests inspect the latest value for each key alongside the output topic. section 14.1.4
Use a KeyValueStore in stateful Kafka Streams testing to:
- Retrieve a named store through TopologyTestDriver.getKeyValueStore. section 14.1.4
- Read the current value for a key with kvStore.get(key). section 14.1.4
- Compare final state-store contents with the latest reduced output after processing records. section 14.1.4
Inspecting a reduced state store
After creating a TopologyTestDriver and processing scores, retrieve the named store and assert its contents: KeyValueStore<String, Double> kvStore = driver.getKeyValueStore("memory-poker-score-store"); assertThat(kvStore.get("Anna"), is(65.75)); Later inputs update the value for each key, allowing assertions against the final accumulated totals. section 14.1.4
Listing 14.17 Validating the state store contents match the latest output
KeyValueStore<String, Double> kvStore =
driver.getKeyValueStore("memory-poker-score-store"); #1
assertThat(kvStore.get("Anna"), is(65.75)); #2
assertThat(kvStore.get("Matthias"), is(55.8));
assertThat(kvStore.get("Neil"), is(47.43));
A compact listing shows how to retrieve a named KeyValueStore and validate values for several keys. section 14.1.4
