Spring Cloud Stream - Testing
Table of Contents
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
- 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
- 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>