如何为春季集成DSL配置类编写Junit 5测试用例
在这里,我使用的是散点图,并拨打了3个子流程。然后收集它们并汇总它们。我需要为配置类,网关和控制器编写JUNIT测试用例。我是春季整合的新手,所以请帮助我。
代码如下 -
//配置类
@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;
}
Here I'm using Scatter-gather pattern and calling 3 sub-flows parallelly. Then gathering them and aggregating them. I need to write junit test cases for the configuration class, gateway and controller. I'm new to Spring Integration so kindly help me with this.
The code is as follows -
//Configuration class
@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;
}
}
//Gateway interface
@MessagingGateway
public interface LionGateway {
@Gateway(requestChannel = "flow.input", replyChannel = "output-flow")
List<?> echo(LionRequest lionRequest);
}
//Controller class
@Autowired private LionGateway lionGateway;
@PostMapping(value = "/invoke-integration")
public String invokeIntegrationFlow(@RequestBody LionRequest lionRequest) {
String response = lionGateway.echo(lionRequest).toString();
return response;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@configuration
测试中没有必要:它只是注册bean。因此,您可能只需要专注于与测试中这些豆类的互动。Controller
您可以通过MOCKMVC: https://docs.spring.io/spring-framework/docs/current/current/reference/html/html/testing.html#spring-mvc-mvc-mvc-mvc-mvc-mvc-framework 。可能
@messaginggateway
可能只用控制器测试覆盖,因为看起来您只需从该@PostMapping
方法调用它即可。要测试春季集成终点,您需要确定要多么刻薄的态度。但是,我们使用
opoctegration
的测试框架可能可以帮助您: https://docs.spring.io/spring-integration/docs/current/referent/referent/html/testing.html#test-context-contextThere is no need in the
@Configuration
testing: it just registers beans. So, you probably need just concentrate on the interaction with those beans in your tests.The
Controller
you can test via MockMvc: https://docs.spring.io/spring-framework/docs/current/reference/html/testing.html#spring-mvc-test-framework.Probably the
@MessagingGateway
could be just covered with the controller testing since it looks like you just call it from that@PostMapping
method.To test Spring Integration endpoints you need to decide how granualy you'd like to go. But probably our Testing Framework with the
MockIntegration
can help you: https://docs.spring.io/spring-integration/docs/current/reference/html/testing.html#test-context