0

This is my code and I am looking to test it. Been looking at a few things (TopologyTestDriver, etc.) but could not get any to work. This example is directly from https://docs.spring.io/spring-cloud-stream/docs/current/reference/html/spring-cloud-stream-binder-kafka.html#_programming_model

Is there a simple, example unit test? Do I even need the kafka stuff or can I just directly test this function

  @Bean
  public Function<KStream<Object, String>, KStream<?, WordCount>> process() {

    return input -> input
                .flatMapValues(value -> Arrays.asList(value.toLowerCase().split("\\W+")))
                .map((key, value) -> new KeyValue<>(value, value))
                .groupByKey(Serialized.with(Serdes.String(), Serdes.String()))
                .windowedBy(TimeWindows.of(5000))
                .count(Materialized.as("word-counts-state-store"))
                .toStream()
                .map((key, value) -> new KeyValue<>(key.key(), new WordCount(key.key(), value,
                        new Date(key.window().start()), new Date(key.window().end()))));
  }
Papi Abi
  • 173
  • 1
  • 10
  • Have you looked at all of the test cases we have in Kafka and KafkaStreams binder? They are all using embedded Kafka, so you can model your tests the same way – Oleg Zhurakousky Aug 30 '22 at 12:51
  • Please see these tests also: https://github.com/spring-cloud/spring-cloud-stream-samples/tree/main/kafka-streams-samples/kafka-streams-word-count/src/test/java/kafka/streams/word/count – sobychacko Aug 30 '22 at 14:00
  • And these tests as well: https://github.com/spring-cloud/spring-cloud-stream-samples/tree/main/kafka-streams-samples/kafka-streams-inventory-count/src/test/java/kafka/streams/inventory/count – sobychacko Aug 30 '22 at 14:01

0 Answers0