当前位置: 首页 > 知识库问答 >
问题:

出站Httpendpoint的Spring集成测试

裴俊迈
2023-03-14

我是Spring集成项目的新手,现在我需要用JavaDSL创建一个流并对其进行测试。我想出了这些流程。第一个应该由cron运行并调用第二个,后者调用HTTPendpoint并将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。替换信息Handlerfor(“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'

我在这里做错了什么吗?我假设我对集成流本身有问题,或者我的测试方法有问题。

共有1个答案

轩辕弘雅
2023-03-14

错误是正确的。bulkEndpoint本身不是endpoint。它实际上是一个消息处理程序。endpoint是从创建的。句柄(bulkEndpoint)。见文件:https://docs.spring.io/spring-integration/docs/current/reference/html/overview.html#finding-html" target="_blank">java和dsl配置的类名,以及https://docs.spring.io/spring-integration/docs/current/reference/html/testing.html#testing-嘲笑。

所以,为了让它工作,你需要做这样的事情:

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

然后在测试中:

mockIntegrationContext.substituteMessageHandlerFor("actualEndpoint", mockHandler);

您可能还需要考虑在测试时不要启动污染流,您可以手动将消息发送到污染通道中。因此,与您想要测试的内容没有冲突。因此,您还可以将id()添加到e.poller(p-

 类似资料:
  • 我正在尝试将spring集成配置为向队列发送消息,然后接收消息,即非常简单的事情: 我认为解耦所必需的是在流程的两端都有一个消息网关。因此,我的第一次尝试(有效)如下所示: 其中MessageReceiverHandler()是扩展AbstractMessageHandler的bean。 所以上面我们有一个用于出站消息的消息网关。我假设我们也应该有一个用于入站消息的网关,允许我们将传入消息处理与应

  • 考虑使用Spring集成进行动态入站/出站配置。使用我们的系统的客户可能有n个,每个客户都有自己的入站FTP/webservice配置,以便将文件拉入我们的系统进行处理。同样,在处理完这些数据之后,每个客户都可以进行出站FTP/webservice配置,需要在其中推送报告(最终结果)。spring集成是否适合这种情况?如果是的话,请你推荐或指出其中的任何一个例子。Webservice包括REST和

  • 我有一个 FileUpload 事件,应该将其发送到 http:outbound upload URL。为此,我必须首先对登录 URL 进行身份验证并获取响应,并设置要执行的出站上传 URL 的会话 ID。在我的情况下,我有一个事件侦听器,它侦听应用程序以发布文件上传事件。发布后,我的侦听器可以拾取并执行流。我正在尝试了解如何实现这一点,因为文件上传对象需要保留,直到登录响应返回。谢谢!

  • 假设您有一个具有入站HTTP接口的应用程序(我们称之为B)(需要使用HTTP)。将来,您将从几个不同的其他应用程序中调用它,但目前您只想开发一个客户端(让我们称之为)。因此,在A中,有一个出站HTTP网关: 是否可以在这两个应用程序之间共享代码,例如HTTP API定义,如路径(例如“/你好”)、方法(GET、POST、PUT…),可能是参数/其类型/响应?如果是,如何? 我想使用JavaDSL,

  • 我正在开发一个关于Spring集成的POC,使用如下。 从远程JMS队列订阅输入消息(A) 将输入消息(A)转换为(B) 使用(B)调用远程Web服务并接收响应 我的spring int-config-xml有以下内容 在我的Spring集成proj工作区中拥有所有jaxb生成的源代码。 在STS 3.8中执行此操作时。3,将抛出以下错误。 不确定我的代码中有什么错误。任何解决这一问题的帮助都是高

  • 我有一个要求,在我必须使用不同的有效负载值对Httpendpoint进行Http出站调用的循环中,调用函数不必等待从出站调用收到的响应,因此基本上出站调用将在循环中异步发生。 有没有办法用Http.outbound网关