testing - 用他们的 test-binder 测试 spring-cloud-stream 总是抛出 MessageDeliveryException: Dispatcher has no subscribers for channel
问题描述
我正在尝试为我的消息生产者编写测试。但是每当我尝试使用注入的绑定发送消息时,我都会收到以下错误
Caused by: org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers, failedMessage=GenericMessage [payload=..., headers={spanTraceId=0761b2d61b665041, spanId=e3b298682fefe198, spanParentSpanId=549ea87126d2484d, nativeHeaders={X-B3-TraceId=[0761b2d61b665041], spanTraceId=[0761b2d61b665041], X-B3-SpanId=[e3b298682fefe198], spanId=[e3b298682fefe198], X-B3-ParentSpanId=[549ea87126d2484d], spanParentSpanId=[549ea87126d2484d], X-B3-Sampled=[0], spanSampled=[0]}, X-B3-SpanId=e3b298682fefe198, X-B3-ParentSpanId=549ea87126d2484d, X-B3-Sampled=0, X-B3-TraceId=0761b2d61b665041, id=0c885a37-a1b1-fc0a-1314-e403ec1bffdb, spanSampled=0, contentType=application/json, timestamp=1589551815143}]
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:138)
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:105)
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:73)
... 42 more
由于我正在测试消息生产者,因此我的代码中自然没有消费者。我该如何解决这个异常?
这是我创建的一个最小测试类,它显示了相同的行为:
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
@SpringBootTest(classes = {MessagingTest.SampleConfiguration.class, Application.class, EventMockConfiguration.class})
public class MessagingTest {
@Autowired
private LibraryMessageSource libraryMessageSource;
@Autowired
private InputDestination input;
@Autowired
private OutputDestination output;
@Test
public void test() throws Exception {
libraryMessageSource.creations().send(MessageBuilder.withPayload("test").build());
}
@SpringBootApplication
@Import({TestChannelBinderConfiguration.class})
public static class SampleConfiguration {
}
}
注意:Application.class 是我的应用程序和 EventMock 的主类,它使用特定的模拟覆盖上下文中的 bean。
public interface LibraryMessageSource {
String LIBRARY_CREATE = "messages-library-create";
String LIBRARY_UPDATE = "messages-library-update";
@Input(LIBRARY_CREATE)
MessageChannel creations();
@Input(LIBRARY_UPDATE)
MessageChannel updates();
}
解决方案
因为我正在测试消息生产者
您的应用程序有消费者,而不是生产者
@Input(INPUT)
MessageChannel events();
你还没有@StreamListener
订阅它。
@Test
public void test() throws Exception {
libraryMessageSource.events().send(MessageBuilder.withPayload("test").build());
}
推荐阅读
- android - 在 Java Android 中解析 JSON 对象时出错
- rxjs6 - rxJS 中的 last 和 publishLast 运算符有什么区别?
- git - 仅在本地存储库上应用预提交?
- docker - 错误:无法设置日志节点进程的优先级 6520
- javascript - 在 Anychart 图中设置最大和最小 xScale 值会导致异常
- arm64 - 如何在 arm64 板上运行 LXC Ubuntu 容器
- javascript - 在 FlatList React-native 中获取 Promise 作为返回值
- c# - 使用 C# 添加 entityFramework 部分
- node.js - 错误:4 DEADLINE_EXCEEDED:已超过截止日期
- android - Webview 作为水平寻呼机 - Android