我是 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 本身有问题,或者我的测试方法有问题。