当我使用 Spring Boot 和 Spring Integration 编写以下简单的 IntegrationFlow 时:
@Bean
IntegrationFlow itf() {
return IntegrationFlows.from(Tcp.inboundAdapter(Tcp.netServer(1111)).id("Inbound"))
.transform(p -> p) //more complex transform and filter chain
.handle(Tcp.outboundAdapter(Tcp.netClient("localhost", 2222)), a -> a.id("Outbound")).get();
}
我可以编写一个 UnitTest 并尝试使用以下代码模拟 Inbound 和 OutboundAdapter:
@SpringIntegrationTest(noAutoStartup = {"Inbound", "Outbound"})
@SpringBootTest
public class ApplicationTests {
@Autowired
MockIntegrationContext mockIntegrationContext;
@Test
public void testIntegrationFlow() {
mockIntegrationContext.substituteMessageSourceFor("Inbound", (MessageSource<String>) () -> MessageBuilder.withPayload("Hello").build());
mockIntegrationContext.substituteMessageHandlerFor("Outbound", p -> System.out.println("Hallo"));
}
}
这将引发异常,因为 inboundAdapter 的类型不是 SourcePollingChannelAdapter
。
有什么方法可以用 Mock 替换 Tcp.inboundAdapter
吗?
如果我的入站适配器是 MessageProducerSupport
怎么模拟这个?
或者我应该在 TCP 服务器之后添加一个 channel ,并通过绕过 TCP 写入该 channel 来测试我的逻辑?
提前致谢。
最佳答案
MockIntegrationContext
目前不支持替换未轮询 MessageSource
的入站端点,因为我们可以简单地发送到第一个 channel 。
您不需要添加 channel 来执行此操作,只需从流中获取输入 channel ...
@Autowired
IntegrationFlow itf;
...
itf.getInputChannel().send(new GenericMessage<>("foo"));
您还可以通过其 bean 名称 Autowiring channel 本身,@Qualified
"itf.channel#0"
关于java - 如何在 spring-integration 中模拟 InboundAdapter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57364945/