Unit testing with Spring Cloud Stream

Spring Cloud Stream provides the spring-cloud-stream-test-binder dependency, allowing us to easily write unit tests for applications using Spring Cloud Stream without having to call actual message brokers.

You can declare and use this spring-cloud-stream-test-binder dependency as follows:

I’ll use the example project from the previous tutorial as an illustration!

To write unit tests for Spring Cloud Stream, we’ll annotate the test class with the annotation @EnableTestBinder:

The @EnableTestBinder annotation will prompt Spring to create two beans, InputDestination and OutputDestination, allowing us to simulate sending and receiving messages to a message broker with Spring Cloud Stream. InputDestination will simulate receiving messages from the message broker to the application, while OutputDestination will simulate sending messages from the application to the message broker.

I will create a new application.yml file in the src/test/resources directory to use for this testing!

InputDestination

As I mentioned above, the bean of the InputDestination class will simulate receiving messages from a message broker to the application.

Currently, in the example project, I have defined a bean of the Consumer interface to receive messages from the message broker:

For testing, to illustrate the assertion, I will add a new class MessageStore with the annotation @Component:

So that when it receives a message from the message broker, this MessageStore class will record that message as follows:

I will define the binding for the Consumer interface in the application.yml file in the src/test/resources directory as follows:

In the unit test, we will ingest the bean of the InputDestination class, send a message to the destination, and assert the result as follows:

The send() method of the InputDestination class has two parameters. The first parameter is the message we want to send, and the second parameter is the name of the Exchange in RabbitMQ or the name of the Topic in Apache Kafka that the application will receive the message from. This InputDestination class also has several other send() methods, but I recommend using the send() method with two parameters as shown above, so that the test can work in any case.

Run this test, and you will see the following pass result:

OutputDestination

The OutputDestination class will simulate sending messages from the application to the message broker.

For example, you can define a bean of the Supplier interface as I did in the previous tutorial:

Định nghĩa binding cho nó:

And you can write tests using the OutputDestination class as follows:

The receive() method of the OutputDestination class has two parameters: a timeout and the name of the Exchange or Topic that will receive the message. Similar to the InputDestination class, the OutputDestination class also has several other receive() methods, but I recommend using the receive() method with two parameters as shown above, so that the test can work in any case.

The results of running this test will be as follows:

So, in this tutorial, I’ve shown you how to use the InputDestination and OutputDestination classes to write unit tests in applications using Spring Cloud Stream. Not having to call the external message broker when running unit tests will be very helpful!

You can watch the video here:

 

Add Comment