Add @EnableTestBinder annotation (#2947)

* Add @EnableTestBinder annotation
* Use @EnableTestBinder in tests
* Update copyright and author tag
* Add javadoc
* Update explanation to use @EnableTestBinder

Resolves https://github.com/spring-cloud/spring-cloud-stream/issues/2931
This commit is contained in:
kutmk
2024-05-14 00:29:00 +09:00
committed by GitHub
parent ee3faf605a
commit 1f41055917
7 changed files with 85 additions and 53 deletions

View File

@@ -48,7 +48,7 @@ The following listing shows the corresponding test:
[source,java]
----
@SpringBootTest(classes = SampleApplication.class)
@Import({TestChannelBinderConfiguration.class})
@EnableTestBinder
class BootTestStreamApplicationTests {
@Autowired
@@ -70,4 +70,3 @@ class BootTestStreamApplicationTests {
Spring Cloud Stream provides a number of abstractions and primitives that simplify the writing of message-driven microservice applications.
The rest of this reference manual provides additional details.

View File

@@ -13,7 +13,7 @@ This test binder acts as a bridge between _unit_ and _integration_ testing and i
[[test-binder-configuration]]
=== Test Binder configuration
To enable Spring Integration test binder, all you need is to add it as a dependency.
To enable Spring Integration test binder, you need to add it as a dependency and annotate your class with `@EnableTestBinder`.
***Add required dependencies***
@@ -38,7 +38,8 @@ testImplementation("org.springframework.cloud:spring-cloud-stream-test-binder")
[[test-binder-usage]]
=== Test Binder usage
Now you can test your microservice as a simple unit test
Now you can test your microservice as a simple unit test.
To enable the Test Binder, annotate your class with `@EnableTestBinder`.
[source,java]
----
@@ -58,7 +59,7 @@ public class SampleStreamTests {
}
@SpringBootApplication
@Import(TestChannelBinderConfiguration.class)
@EnableTestBinder
public static class SampleConfiguration {
@Bean
public Function<String, String> uppercase() {
@@ -201,7 +202,7 @@ public void samplePollingTest() {
System.out.println("Message 3: " + new String(destination.receive().getPayload()));
}
@Import(TestChannelBinderConfiguration.class)
@EnableTestBinder
@EnableAutoConfiguration
public static class SamplePolledConfiguration {
@Bean
@@ -260,13 +261,3 @@ Message 3: MY OWN DATA 20BF2E64-7FF4-4CB6-A823-4053D30B5C74
NOTE: DO NOT name this bean `messageSource` as it is going to be in conflict with the bean of the same name (different type)
provided by Spring Boot for unrelated reasons.
[[special-note-on-mixing-test-binder-and-regular-middleware-binder-for-testing]]
== Special Note on Mixing Test Binder and Regular Middleware Binder for Testing
The Spring Integration based test binder is provided for testing the application without involving an actual middleware based binder such as the Kafka or RabbitMQ binder.
As described in the sections above, the test binder helps you to verify the application behavior quickly by relying on the in-memory Spring Integration channels.
When the test binder is present on the test classpath, Spring Cloud Stream will try to use this binder for all testing purposes wherever it needs a binder for communication.
In other words, you cannot mix both the test binder and a regular middleware binder for testing purposes in the same module.
After testing the application with the test binder, if you want to continue doing further integration tests using the actual middleware binder, it is recommended to add those tests that use the actual binder in a separate module so that those tests can make the proper connection to the actual middleware rather than relying on the in-memory channels provided by the test binder.