如何编写用于Spring-Batch-Integration的集成测试?

时间:2019-06-25 18:12:31

标签: spring-batch spring-integration-sftp

我使用的是与spring-batch捆绑在一起的spring-integration,在尝试编写集成测试来测试整个流程而不仅仅是单个配置时遇到了麻烦。

我为此测试创建了嵌入式Sftp服务器,并尝试将消息发送到sftpInboundChannel-消息已发送,但是什么也没有发生,但是当我将此消息发送到下一个通道(在sftpInboundChannel之后)时,一切正常。同样,即使我使用@TestPropertySource批注,我也无法加载测试源属性。

这是我的课堂注释


@TestPropertySource(properties = {
  //here goes all the properties
})
@EnableConfigurationProperties
@RunWith(SpringRunner.class)
@Import({TestConfig.class, SessionConfig.class})
@ActiveProfiles("it")
@SpringIntegrationTest
@EnableIntegration
@SpringBootTest
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)

这是我班级的身体

    @Autowired
    private PollableChannel sftpInboundChannel;

    @Autowired
    private SessionFactory<ChannelSftp.LsEntry> defaultSftpSessionFactory;

    @Autowired
    private EmbeddedSftpServer server;

@Test
    public void shouldDoSmth() {

        RemoteFileTemplate<ChannelSftp.LsEntry> template;
        try {
            template = new RemoteFileTemplate<>(defaultSftpSessionFactory);
            SftpTestUtils.moveToRemoteFolder(template);

            final List<ChannelSftp.LsEntry> movedFiles = SftpTestUtils.listFilesFromDirectory("folder/subfolder", template);
            log.info("Moved file {}", movedFiles.size());

            final MessageBuilder<String> messageBuilder = MessageBuilder.withPayload("Sample.txt") // path to file
                .setHeader("file_Path", "Sample.txt")

            boolean wasSent = this.sftpInboundChannel.send(messageBuilder.build());

            log.info("Was sent to sftpInboundChannel channel {}", wasSent);

            log.info("message {}", messageBuilder.build());
        } finally {
            SftpTestUtils.cleanUp();
        }
    }

1 个答案:

答案 0 :(得分:0)

对于不读取属性文件的情况,一种解决方案是在Test类中添加如下内容:

@BeforeClass
    public static void beforeClass() {
        System.setProperty("propertyfile",  "nameOfFile.properties");
     }

第二种方法是在添加标记的地方创建xml(或类)配置:

<context:property-placeholder
    location="nameOfFile.properties"
    ignore-resource-not-found="true" system-properties-mode="OVERRIDE" />

,您的文件将被本地化。

属性文件应位于资源文件夹中。