首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何编写spring-batch-integration的集成测试?

如何编写spring-batch-integration的集成测试?
EN

Stack Overflow用户
提问于 2019-06-26 02:12:31
回答 1查看 80关注 0票数 0

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

我已经为这项测试创建了嵌入式Sftp Server,并尝试将消息发送到sftpInboundChannel -消息已发送,但没有任何反应,但当我将此消息发送到下一个通道(在sftpInboundChannel之后)时,消息正常。此外,即使我使用@TestPropertySource注解,我也无法加载测试源属性。

这是我的类注释

代码语言:javascript
复制
@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)

这是我的类体

代码语言:javascript
复制
    @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();
        }
    }
EN

回答 1

Stack Overflow用户

发布于 2019-07-04 00:12:21

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

代码语言:javascript
复制
@BeforeClass
    public static void beforeClass() {
        System.setProperty("propertyfile",  "nameOfFile.properties");
     }

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

代码语言:javascript
复制
<context:property-placeholder
    location="nameOfFile.properties"
    ignore-resource-not-found="true" system-properties-mode="OVERRIDE" />

你的文件将被本地化。

属性文件应该在资源文件夹内。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56759840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档