1

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!

nmarques
  • 151
  • 12

1 Answers1

0

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

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • thanks a lot Gary. I wasn't sure what was the best way to proceed as I see different ways of testing things such as: https://cloud.spring.io/spring-cloud-static/spring-cloud-stream/3.0.4.RELEASE/reference/html/spring-cloud-stream.html#_testing – nmarques May 21 '20 at 14:27
  • 1
    It depends on what you are testing; the old test binder referenced there is ok for basic unit tests; the newer test binder is good for basic integration tests; but for full integration tests (including Kafka producer/consumer configuration), you need a real broker. An alternative to the embedded broker is [Testcontainers](https://www.testcontainers.org/). – Gary Russell May 21 '20 at 14:47