【发布时间】:2023-11-05 08:11:01
【问题描述】:
我尝试模拟 pollEnrich 的端点,但没有成功。使用adviceWith 模拟对.to("") 和.enrich("") 来说效果很好,但是对于pollEnrich 我遇到了错误:
org.apache.http.conn.HttpHostConnectException: Connect to localhost:8983
我不明白为什么adviceWith 不适合pollEnrich
这是模拟代码:
public class PollEnricherRefTest extends CamelTestSupport {
public class SampleMockRoute extends RouteBuilder {
public void configure() throws Exception {
System.out.println("configure");
from("direct:sampleInput")
.log("Received Message is ${body} and Headers are ${headers}")
//.to("http4://localhost:8983/test")
.pollEnrich("http4://localhost:8983/test", (exchange1, exchange2) -> {
return exchange1;
})
.log("after enrich ${body} ")
.to("mock:output");
}
}
@Override
public boolean isUseAdviceWith() {
return true;
}
@BeforeEach
public void beforeAll() throws Exception {
AdviceWithRouteBuilder mockHttp4 = new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
interceptSendToEndpoint("http4://localhost:8983/test")
.log("call MOCK HTTP").skipSendToOriginalEndpoint();
}
};
context = new DefaultCamelContext();
context.addRoutes(new SampleMockRoute());
context.getRouteDefinitions().get(0).adviceWith(context, mockHttp4);
template = context.createProducerTemplate();
}
@Test
public void sampleMockTest() throws InterruptedException {
try {
context.start();
String expected = "Hello";
MockEndpoint mock = getMockEndpoint("mock:output");
mock.expectedBodiesReceived(expected);
String input = "Hello";
template.sendBody("direct:sampleInput", input);
mock.assertIsSatisfied();
context.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
}
【问题讨论】:
标签: mocking apache-camel endpoint