[英]How to write Junit 5 test cases for Spring integration dsl configuration class
在这里,我使用 Scatter-gather 模式并并行调用 3 个子流。 然后收集它们并聚合它们。 我需要为配置类、网关和控制器编写 junit 测试用例。 我是 Spring Integration 的新手,所以请帮我解决这个问题。
代码如下 -
//配置类
@Configuration
public class IntegrationConfiguration {
@Autowired LoansServiceImpl loansService;
long dbId = new SequenceGenerator().nextId();
// Main flow
@Bean
public IntegrationFlow flow() {
return flow ->
flow.split()
.log()
.channel(c -> c.executor(Executors.newCachedThreadPool()))
.convert(LoanProvisionRequest.class)
.scatterGather(
scatterer ->
scatterer
.applySequence(true)
.recipientFlow(flow1())
.recipientFlow(flow2())
.recipientFlow(flow3()),
gatherer -> gatherer.releaseLockBeforeSend(true))
.log()
.aggregate(a -> a.outputProcessor(MessageGroup::getMessages))
.channel("output-flow");
}
// flow1
@Bean
public IntegrationFlow flow1() {
return integrationFlowDefination ->
integrationFlowDefination
.channel(c -> c.executor(Executors.newCachedThreadPool()))
.handle(
message -> {
try {
lionService.saveLionRequest(
(LionRequest) message.getPayload(), String.valueOf(dbId));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
});
}
// flow2
@Bean
public IntegrationFlow flow2() {
return integrationFlowDefination ->
integrationFlowDefination
.channel(c -> c.executor(Executors.newCachedThreadPool()))
.handle(
message ->
lionService.getData(
(LionRequest) message.getPayload(), SourceSystem.PROVISION))
.log();
}
// flow3
@Bean
public IntegrationFlow flow3() {
return integrationFlowDefination ->
integrationFlowDefination
.channel(c -> c.executor(Executors.newCachedThreadPool()))
.handle(
message ->
lionService.prepareCDRequest(
(LionRequest) message));
}
@Bean
public MessageChannel replyChannel() {
return MessageChannels.executor("output-flow", outputExecutor()).get();
}
@Bean
public ThreadPoolTaskExecutor outputExecutor() {
ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
pool.setCorePoolSize(4);
pool.setMaxPoolSize(4);
return pool;
}
}
//网关接口
@MessagingGateway
public interface LionGateway {
@Gateway(requestChannel = "flow.input", replyChannel = "output-flow")
List<?> echo(LionRequest lionRequest);
}
//控制器类
@Autowired private LionGateway lionGateway;
@PostMapping(value = "/invoke-integration")
public String invokeIntegrationFlow(@RequestBody LionRequest lionRequest) {
String response = lionGateway.echo(lionRequest).toString();
return response;
}
@Configuration
测试不需要:它只是注册 bean。 因此,您可能只需要在测试中专注于与这些 bean 的交互。
您可以通过 MockMvc 测试的Controller
: https ://docs.spring.io/spring-framework/docs/current/reference/html/testing.html#spring-mvc-test-framework。
可能@MessagingGateway
可能只是被控制器测试所覆盖,因为看起来您只是从该@PostMapping
方法中调用它。
要测试 Spring Integration 端点,您需要确定您想要的粒度。 但可能我们的带有MockIntegration
的测试框架可以帮助您: https ://docs.spring.io/spring-integration/docs/current/reference/html/testing.html#test-context
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.