我们如何对 spring 集成 xml 配置进行单元测试?

How do we unit test spring integration xml configuration?

我是 spring 集成的新手,目前坚持对我的集成流程进行单元测试。我的流程看起来像这样。

  1. 从 TCP 通道适配器接收一些 XML 格式的数据。
  2. 将其转换为 JSON。
  3. 向亚马逊 sqs 队列发送 JSON 消息。

和XML文件是:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:int="http://www.springframework.org/schema/integration"
   xmlns:int-ip="http://www.springframework.org/schema/integration/ip"
   xmlns:int-aws="http://www.springframework.org/schema/integration/aws"
   xmlns:aws-messaging="http://www.springframework.org/schema/cloud/aws/messaging"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/integration
   http://www.springframework.org/schema/integration/spring-integration.xsd
   http://www.springframework.org/schema/integration/ip
   http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd
   http://www.springframework.org/schema/integration/aws
   http://www.springframework.org/schema/integration/aws/spring-integration-aws.xsd
   http://www.springframework.org/schema/cloud/aws/messaging 
   https://www.springframework.org/schema/cloud/aws/messaging/spring-cloud-aws-messaging.xsd">

<!-- -->
<!-- receive callbacks -->
<!-- -->
<int-ip:tcp-connection-factory
        id="inboundServerTCPFactory1"
        deserializer="customSerializerDeserializer"
        serializer="customSerializerDeserializer"
        type="server"
        port="8085"
        using-nio="true"
        single-use="true"/>

<!-- -->
<!-- receive callbacks -->
<!-- -->
<int-ip:tcp-connection-factory
        id="inboundServerTCPFactory2"
        deserializer="customSerializerDeserializer"
        serializer="customSerializerDeserializer"
        type="server"
        port="8086"
        using-nio="true"
        single-use="true"/>

<!-- -->
<!-- Inbound channel adapter -->
<!-- -->
<int-ip:tcp-inbound-channel-adapter id="inboundChannelAdapter1"
                                    channel="inboundReceivingChannel"
                                    connection-factory="inboundServerTCPFactory1"/>

<int-ip:tcp-inbound-channel-adapter id="inboundChannelAdapter2"
                                    channel="inboundReceivingChannel"
                                    connection-factory="inboundServerTCPFactory2"/>

<!-- -->
<!-- Inbound channel adapter -->
<!-- -->
<int:channel id="inboundReceivingChannel">
    <int:interceptors>
        <int:wire-tap channel="logger"/>
    </int:interceptors>
</int:channel>


<!--Transfrom XML message to Json-->
<int:transformer ref="xmlToJsonTransformer" input-channel="inboundReceivingChannel"
                 method="transform" output-channel="outChannel"/>

<int:channel id="outChannel" />

<!--<aws-messaging:sqs-async-client id="amazonSqs"/>-->

<int-aws:sqs-outbound-channel-adapter id= "out"
                                      sqs="sqs"
                                      channel="outChannel"
                                      queue="SendQueue"/>
</beans>

如何模拟每个组件并对整个流程进行单元测试?

请参阅 Spring 集成测试支持文档:https://docs.spring.io/spring-integration/docs/current/reference/html/testing.html#testing。该框架在 Spring JUnit 测试 class 上通过 @SpringIntegrationTest 标记为我们提供了 MockIntegrationContextMockIntegration 工厂允许我们创建各自的模拟并存根它们的处理逻辑。然后你可以用你的模拟替换端点 bean 等等。