java - 如何在 spring-integration 中模拟 InboundAdapter?

标签 java spring-boot 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 服务器之后添加一个 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/

相关文章:

java - 如何在 spring boot 中创建一个 Tcp 连接来接受连接?

mockito - 使用 Http.outboundGateway() 和配置的 RestTemplate 测试 spring 集成流程

Java - 循环的 Perlin 噪音

java - Java中某些数字的排列

spring - 为什么这个最大文件大小配置不起作用?

java - 在 SpringBoot 应用程序的 ControllerAdvice 中访问 HttpSession

java - Spring Boot 测试类不会注入(inject) bean

groovy - Spring Integration webflux - 验证输入 JSON 正文

java - 具有 Maven 依赖项而不是 TargetPlatform 的 Eclipse OSGi 或 RCP 应用程序

java - 自动化/处理第 5 个下拉列表 url https ://jedwatson. github.io/react-select/named as Github users(Aysnc with fetch.js)