Search code examples
spring-cloud-streamspring-cloud-stream-binder-kafkaspring-cloud-function

Spring Cloud Function and Kafka


I'm struggling to understand how I should go about testing an application that makes use of Kafka Binder while also using Spring Cloud function.

Let's use this very simple example:

@SpringBootApplication
public class DemoKafkaApplication {

public static void main(String[] args) {
    SpringApplication.run(DemoKafkaApplication.class, args);
}

@Bean
public Function<String, String> uppercase() {
    return value -> value.toUpperCase();
}

}

And on my application.yaml:

spring.cloud:
  stream:
    function:
      definition: uppercase
    bindings:
      uppercase-in-0:
      destination:  uppercase-topic

How would I go about testing this? If I were using @StreamListener and a Channels list, I would do something like this:

 channels.uppercase().send(MessageBuilder.withPayload("test").build());

 messageCollector.forChannel(channels.uppercaseOutput()).poll(5, TimeUnit.SECONDS);

However, for Spring Cloud Function that is not the case. Any help is very much welcome as I can't find anything in the offical docs or samples!


Solution

  • See the "Testing with an Embedded Kafka Broker" sample:

    https://github.com/spring-cloud/spring-cloud-stream-samples/tree/master/testing-samples/test-embedded-kafka

    and the Spring for Apache Kafka documentation for more up-to-date information about the embedded broker.

    https://docs.spring.io/spring-kafka/docs/2.4.6.RELEASE/reference/html/#testing