我是spring集成项目的新手,现在我需要用javadsl创建一个流并进行测试。我想出了这些流程。第一个应该由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'
我做错什么了吗?我假设集成流本身有问题,或者我的测试方法有问题。
1条答案
按热度按时间zsbz8rwp1#
错误是正确的。这个
bulkEndpoint
本身不是终结点。这真是一场灾难MessageHandler
. 端点是从.handle(bulkEndpoint)
. 见文件:https://docs.spring.io/spring-integration/docs/current/reference/html/overview.html#finding-java和dsl配置的类名以及https://docs.spring.io/spring-integration/docs/current/reference/html/testing.html#testing-嘲笑。所以,要让它发挥作用,你需要这样做:
然后在测试中:
你可能还需要思考一下,不要这样
pollerFlow
当您测试它时,只要您将消息发送到pollingChannel
手动。因此,与您想要测试的内容没有冲突。为此,您还添加了id()
进入你的e.poller(p -> p.cron(this.cron))
使用@SpringIntegrationTest(noAutoStartup)
在考试前让它停下来。我看到你试过了noAutoStartup = {"pollerFlow"}
,但这对静态流没有帮助。在这种情况下,您确实需要停止一个实际的端点。