2

我是 Spring Integration 项目的新手,现在我需要使用 Java DSL 创建一个流并对其进行测试。我想出了这些流程。第一个应该由 cron 运行并调用第二个,它调用 HTTP 端点并将 XML 响应转换为 POJO:

  @Bean
  IntegrationFlow pollerFlow() {
    return IntegrationFlows
        .from(() -> new GenericMessage<>(""),
            e -> e.poller(p -> p.cron(this.cron)))
        .channel("pollingChannel")
        .get();
  }

  @Bean
  IntegrationFlow flow(HttpMessageHandlerSpec bulkEndpoint) {
    return IntegrationFlows
        .from("pollingChannel")
        .enrichHeaders(authorizationHeaderEnricher(user, password))
        .handle(bulkEndpoint)
        .transform(xmlTransformer())
        .channel("httpResponseChannel")
        .get();
  }

  @Bean
  HttpMessageHandlerSpec bulkEndpoint() {
    return Http
        .outboundGateway(uri)
        .httpMethod(HttpMethod.POST)
        .expectedResponseType(String.class)
        .errorHandler(new DefaultResponseErrorHandler());
  }

现在我想测试流程和模拟 HTTP 调用,但努力模拟 HTTP 处理程序,我尝试这样做:

@ExtendWith(SpringExtension.class)
@SpringIntegrationTest(noAutoStartup = {"pollerFlow"})
@ContextConfiguration(classes = FlowConfiguration.class)
public class FlowTests {

  @Autowired
  private MockIntegrationContext mockIntegrationContext;
  @Autowired
  public DirectChannel httpResponseChannel;
  @Autowired
  public DirectChannel pollingChannel;

  @Test
  void test() {
    final MockMessageHandler mockHandler = MockIntegration.mockMessageHandler()
        .handleNextAndReply(message -> new GenericMessage<>(xml, message.getHeaders()));
    mockIntegrationContext.substituteMessageHandlerFor("bulkEndpoint", mockHandler);
    httpResponseChannel.subscribe(message -> {
      assertThat(message.getPayload(), is(notNullValue()));
      assertThat(message.getPayload(), instanceOf(PartsSalesOpenRootElement.class));
    });

    pollingChannel.send(new GenericMessage<>(""));
  }
}

但我总是收到一个错误,即在线:

mockIntegrationContext.substituteMessageHandlerFor("bulkEndpoint", mockHandler);

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'bulkEndpoint' is expected to be of type 'org.springframework.integration.endpoint.IntegrationConsumer' but was actually of type 'org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler'

我在这里做错了吗?我假设我的 IntegrationFlow 本身有问题,或者我的测试方法有问题。

4

1 回答 1

2

错误是正确的。bulkEndpoint本身不是端点。它真的是一个MessageHandler. 端点是从.handle(bulkEndpoint). 请参阅文档:https ://docs.spring.io/spring-integration/docs/current/reference/html/overview.html#finding-class-names-for-java-and-dsl-configuration和https://docs .spring.io/spring-integration/docs/current/reference/html/testing.html#testing-mocks

因此,要使其正常工作,您需要执行以下操作:

.handle(bulkEndpoint, e -> e.id("actualEndpoint"))

然后在测试中:

mockIntegrationContext.substituteMessageHandlerFor("actualEndpoint", mockHandler);

您可能还需要考虑pollerFlow在测试时不要启动它,因为您pollingChannel手动发送消息。因此,与您要测试的内容没有冲突。出于这个原因,您还可以添加一个id() 到您的e.poller(p -> p.cron(this.cron))并使用@SpringIntegrationTest(noAutoStartup)它在您的测试之前停止它。我看到你在尝试noAutoStartup = {"pollerFlow"},但这对静态流没有帮助。在这种情况下,您确实需要停止一个实际的端点。

于 2020-12-15T14:41:37.793 回答