Spring Cloud Stream - Testing

Automated Testing with Spring Cloud Stream

You can easily test your microservice without connecting to a message broker. To achieve it you need to include spring-cloud-stream-test-support to your project dependencies. It contains the TestSupportBinder bean that lets you interact with the bound channels and inspect any messages sent and received by the application.

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-stream-test-support</artifactId>
    <scope>test</scope>
</dependency>

In the test class, we need to declare MessageCollector bean, which is responsible for receiving messages retained by TestSupportBinder.

See

  1. https://github.com/explorer436/programming-playground/blob/main/java-playground/spring-cloud-examples/spring-cloud-stream/usage-detail-sender-rabbit/src/test/java/io/spring/dataflow/sample/usagedetailsenderrabbit/UsageDetailSenderRabbitApplicationTests.java
  2. https://github.com/explorer436/programming-playground/blob/main/java-playground/spring-cloud-examples/spring-cloud-stream/usage-cost-processor-rabbit/src/test/java/io/spring/dataflow/sample/usagecostprocessorrabbit/UsageCostProcessorRabbitApplicationTests.java

Using reactor-test

However, for applications using reactive library, this is a much better option.

Remove this. It is buggy.

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-stream-test-binder</artifactId>
    <scope>test</scope>
</dependency>

Add this:

<dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-test</artifactId>
        <scope>test</scope>
</dependency>

See https://github.com/explorer436/programming-playground/tree/main/java-playground/spring-cloud-examples/spring-cloud-stream/cloud-stream-kafka-playground/src/test/java/com/my/company/cloud_stream_kafka_playground


Links to this note