如何对SpringIntegration的int文件进行单元测试:入站通道适配器



我有一个简单的Spring Integration流,我在其中读取XML文件,并将其内容发送到另一个频道并进行其他处理。

处理完成后,我需要将输入文件移动到归档/错误目录中。

file-integration.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-file="http://www.springframework.org/schema/integration/file"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/file https://www.springframework.org/schema/integration/file/spring-integration-file.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<import resource="transaction-manager-configuration.xml"/>
<context:property-placeholder location="classpath:app.properties"/>
<int:channel id="inputFileChannel"/>
<int-file:inbound-channel-adapter channel="inputFileChannel"
directory="${inputDir}"
filename-pattern="*.xml">
<int:poller fixed-delay="1000">
<int:transactional transaction-manager="txManager" synchronization-factory="syncFactory"/>
</int:poller>
</int-file:inbound-channel-adapter>
<int-file:file-to-string-transformer input-channel="inputFileChannel"
output-channel="contentChannel"/>
<int:channel id="contentChannel"/>
<int:channel id="inputFileSuccessChannel"/>
<int:channel id="inputFileErrorChannel"/>
<!-- Move file after being processed -->
<int:transaction-synchronization-factory id="syncFactory">
<int:after-commit channel="inputFileSuccessChannel"/>
<int:after-rollback channel="inputFileErrorChannel"/>
</int:transaction-synchronization-factory>
<int-file:outbound-channel-adapter channel="inputFileSuccessChannel"
directory="${archiveDir}"
delete-source-files="true"/>
<int-file:outbound-channel-adapter channel="inputFileErrorChannel"
directory="${errorDir}"
delete-source-files="true"/>
</beans>

我的问题:

  1. 在单元测试上下文中,我如何创建&在不依赖真实文件系统的情况下,在inputFileChannel中的内存中推送文件
  2. 如何测试我的输入文件是否已被推送到inputFileSuccessChannel/inputFileErrorChannel?如何模拟<int-file:outbound-channel-adapter/>
  3. 同样,在不依赖真实文件系统的单元测试上下文中,我如何测试内存中的输入文件是否已被删除(如delete-source-files="true"语句所配置的(

目前,我有一个JUnit 5测试类FileIntegrationTest.java:

@ExtendWith(SpringExtension.class)
@ContextConfiguration
@PropertySource("classpath:app.properties")
@SpringIntegrationTest
public class FileIntegrationTest {
@Autowired
MessageChannel inputFileChannel;
@Autowired
QueueChannel contentTestChannel;
@Test
void shouldReadInputFile_thenMoveIt() {
// arrange
File inputFile = new … // question 1
Message<File> inputMessage = MessageBuilder.withPayload(file).build();
// act
inputFileChannel.send(inputMessage);
// assert
assertThat(contentTestChannel.getQueueSize(), is(1));
assertThat(inputFileSuccessChannel contains my input file); // question 2
assertThat(inputFile has been deleted); // question 3
}
}

及其上下文文件FileIntegrationTest-context.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd">
<import resource="classpath:/META-INF/spring/integration/file-integration.xml"/>
<int:bridge input-channel="contentChannel"
output-channel="contentTestChannel"/>
<int:channel id="contentTestChannel">
<int:queue/>
</int:channel>
</beans>

官方的Spring测试支持页面提到了mock的使用,但我对如何使用它感到困惑https://github.com/spring-projects/spring-integration-samples当然,但我可以找到一个合适的解决方案。

  1. 在通道中添加一个通道拦截器,或者参阅有关模拟端点的文档
  2. 如果您将测试文件发送到通道;当测试完成时,您可以检查是否仍然存在

最新更新