繁体   English   中英

如何在Spring集成中模拟InboundAdapter?

[英]How to mock an InboundAdapter in spring-integration?

当我用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 Server之后添加一个通道并通过绕过TCP写入该通道来测试我的逻辑?

提前致谢。

MockIntegrationContext当前不支持替换未轮询MessageSource入站端点,因为是的,我们可以简单地发送到第一个通道。

您不需要添加通道即可,只需从流中获取输入通道即可。

@Autowired
IntegrationFlow itf;

...

    itf.getInputChannel().send(new GenericMessage<>("foo"));

您还可以自动连接通道本身,通过其Bean名称@Qualified

"itf.channel#0"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM