我尝试为 pollEnrich 模拟端点,但没有成功。用adviceWith模拟对于.to("")
和.enrich("")
来说可以正常工作,但是对于pollEnrich,我遇到了错误:
org.apache.http.conn.HttpHostConnectException:连接到本地主机: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();
}
}
}
答案 0 :(得分:0)
您可以尝试在configure()中使用weavyByType。唯一的问题是,它将用该路由中的以下模拟网址替换所有pollEnrich模式。如果您的路线中只有一个pollEnrich,这应该可以达到您的目的。
weaveByType(PollEnrichDefinition.class).replace().to("mock:mock-url");
您也可以尝试在路由中为该特定的pollEnrich设置ID。
.pollEnrich("http4://localhost:8983/test", (exchange1, exchange2) -> {
return exchange1;
}).id("myPollEnrichId")
一旦完成,您就可以使用weavyById模拟特定的pollEnrich。
weaveById("myPollEnrichId").replace().to("mock:mock-url");
答案 1 :(得分:0)
对于被替换的路由不调用聚合策略的问题的任何人:您可以将weaveById
与pollEnrich
一起使用,但您还需要添加AggregationStrategy参数(见下文),否则它不会调用它,因为它正在用另一个没有它的不同的 pollEnrich
替换原来的 pollEnrich
。
首先:在 configure
方法中为原始 .pollEnrich("http4://localhost:8983/test", (exchange1, exchange2) -> {
return exchange1;
}).id("my-pollEnrich")
添加一个 id:
weaveById
第二:在测试中,使用 AdviceWithRouteBuilder mockHttp4 = new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
weaveById("my-pollEnrich").replace().pollEnrich("mock:my-pollEnrich-mock", (exchange1, exchange2) -> {
return exchange1;
}).id("my-pollEnrich"); //set the same id, otherwise it won't find when executing "beforeEach" again
}
};
将其发送到设置了聚合策略的模拟路由:
[...]
return validate_input(a)
如果需要在路由中测试实际的聚合策略,最好的方法是为其创建一个单独的类并在测试中重用。