【问题标题】:Spring Integration 5.1 - integration flow test - dslSpring Integration 5.1 - 集成流测试 - dsl
【发布时间】:2019-07-01 16:47:31
【问题描述】:

我已经建立了一个简单的 Spring 集成流程,由以下步骤组成:

  1. 然后定期轮询休息 api
  2. 对payload做一些处理
  3. 并将其放在 Kafka 主题上。

请注意以下代码:

@Component
public class MyIntegrationFlow extends IntegrationFlowAdapter {
    @Override
    protected IntegrationFlowDefinition<?> buildFlow() {
        return from(() -> List.of("pathVariable1", "pathVariable2"), c -> c.poller(Pollers.fixedDelay(5, TimeUnit.SECONDS)))
                .split()
                .handle(httpRequest(), c -> c.advice(new RequestHandlerRetryAdvice()))
                .transform(Tranformers.fromJson(Foo.class))
                .filter(payload -> payload.isValid())
                .log()
                .transform(Tranformers.toJson())
                .channel(Source.OUTPUT); // output channel for kafka topic
    }

    private HttpMessageHandlerSpec httpRequest() {
        return Http.outboundGateway("http://somehost:8080/{pathVariable}")
                .httpMethod(GET)
                .uriVariable("pathVariable", Message::getPayload)
                .expectedResponseType(String.class);
    }
}

这非常有效,但是,我正在努力想出一些好的测试。

  • 我应该如何模拟外部 REST API?
  • 我应该如何测试重试策略是否生效以及是否发出了所需数量的 http 请求?
  • 如何更改定期轮询的流(路径变量列表)的MessageSource
  • 如何检查负载是否已成功进入 Kafka 主题?

【问题讨论】:

    标签: spring-integration spring-integration-dsl spring-integration-http


    【解决方案1】:

    问题太多,其中一些需要太宽泛的解释。无论如何,我认为您可以从 Spring Integration Testing Framework 及其documentation 开始。

    1. 我应该如何模拟外部 REST API?

    我认为您可以考虑使用 Spring Framework 中的 Mock MVC 及其MockMvcClientHttpRequestFactory 基于HttpMessageHandlerSpec 注入HttpRequestExecutingMessageHandler

    1. 重试策略确实有效

    好吧,我想同一个模拟 MVC 端点可以验证它被调用了多少次,并且前几次失败以启动重试。

    1. 如何更改消息源

    这正是 Spring 集成测试框架的一部分,其 MockIntegration.mockMessageSource()MockIntegrationContexthttps://docs.spring.io/spring-integration/docs/5.1.6.RELEASE/reference/html/#mockintegration

    1. 进入 Kafka 主题了吗?

    或者你提到的MockIntegration.mockMessageHandler() 来验证是否调用了 Kafka 的端点。或者使用 Spring Kafka 项目中的 Embedded Kafkahttps://docs.spring.io/spring-kafka/docs/2.2.7.RELEASE/reference/html/#embedded-kafka-annotation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-07
      • 2020-11-12
      • 2017-06-28
      • 1970-01-01
      • 2020-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多