[英]How can we mock test IntegrationFlow in spring boot. I am using Spring Integration inside a Spring boot app using Spring Integration File
I have a task where I need to implement IntegrationFlow in Spring boot app and for that I created one project.我有一项任务需要在 Spring Boot 应用程序中实现 IntegrationFlow,为此我创建了一个项目。 Below are 3 classes.
下面是3个班。
IntegrationConfig.java集成配置.java
@Configuration
public class IntegrationConfig {
@Autowired
private Transformer transformer;
@Bean
public IntegrationFlow integrationFlow() {
return IntegrationFlows.from(fileReader(),spec -> spec.poller(Pollers.fixedDelay(500)))
.transform(transformer,"transform")
.handle(fileWriter())
.get();
}
@Bean
public FileWritingMessageHandler fileWriter() {
FileWritingMessageHandler handler = new FileWritingMessageHandler(new File("destination"));
handler.setExpectReply(false);
return handler;
}
@Bean
public FileReadingMessageSource fileReader() {
FileReadingMessageSource source = new FileReadingMessageSource();
source.setDirectory(new File("source"));
return source;
}
} }
Transformer.java变压器.java
@Component
public class Transformer {
public String transform(String filePath) throws IOException {
String content = new String (Files.readAllBytes(Paths.get(filePath)));
return "Transformed: " + content;
}
} }
SpringIntegrationExample.java SpringIntegrationExample.java
@SpringBootApplication
public class SpringIntegrationExample {
public static void main(String[] args){
SpringApplication.run(SpringIntegrationExample.class,args);
}
}
Now there is task to mock tests this.现在有模拟测试的任务。
I am new to test and not sure how to test this using either JUnit or Mockito or both.我是新手,不知道如何使用 JUnit 或 Mockito 或两者来测试。 I tried with JUnit with When().thenReturn() but not able to test IntegrationFlow in that.
我尝试使用带有 When().thenReturn() 的 JUnit,但无法在其中测试 IntegrationFlow。
Can anyone help me on this ?谁可以帮我这个事 ? Thanks in advance.
提前致谢。
A couple notes for your configuration so far:到目前为止,您的配置有几点注意事项:
The FileReadingMessageSource
produces File
as payload for messages. FileReadingMessageSource
生成File
作为消息的有效负载。 The FileWritingMessageHandler
can deal with File
payloads. FileWritingMessageHandler
可以处理File
有效负载。 Therefore you don't need that transform()
in between.因此,您不需要介于两者之间的
transform()
。
Although you just show not the whole flow...虽然你只是没有展示整个流程......
There is Java DSL-specific factory for fluent API: org.springframework.integration.file.dsl.Files
.流式 API 有 Java DSL 特定的工厂:
org.springframework.integration.file.dsl.Files
。
It is not clear what you'd like to mock, but here is a dedicated documentation about testing with Spring Integration: https://docs.spring.io/spring-integration/docs/current/reference/html/testing.html#testing目前尚不清楚您想模拟什么,但这里有一个关于使用 Spring Integration 进行测试的专用文档: https ://docs.spring.io/spring-integration/docs/current/reference/html/testing.html# 测试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.