如何为春季集成DSL配置类编写Junit 5测试用例

发布于 2025-02-11 16:57:58 字数 3210 浏览 2 评论 0原文

在这里,我使用的是散点图,并拨打了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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒绊 2025-02-18 16:57:58

@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-context

There 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文