226.7. 模拟现有端点
可作为 Camel 2.7 提供
Camel 现在允许您在 Camel 路由中自动模拟现有端点。
它的工作原理是 端点仍然在操作中。另一种情况是,注入 Mock 端点并接收消息,然后将消息委派给目标端点。您可以将其视为一种拦截器和委派或端点监听程序。
假设您有以下给定路由:
Route(路由)
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").to("direct:foo").to("log:foo").to("mock:result");
from("direct:foo").transform(constant("Bye World"));
}
};
}
然后,您可以使用 Camel 中的 adviceWith 功能模拟从单元测试给定路由中的所有端点,如下所示:
建议通过 模拟所有端点进行建议
public void testAdvisedMockEndpoints() throws Exception {
// advice the first route using the inlined AdviceWith route builder
// which has extended capabilities than the regular route builder
context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
// mock all endpoints
mockEndpoints();
}
});
getMockEndpoint("mock:direct:start").expectedBodiesReceived("Hello World");
getMockEndpoint("mock:direct:foo").expectedBodiesReceived("Hello World");
getMockEndpoint("mock:log:foo").expectedBodiesReceived("Bye World");
getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
template.sendBody("direct:start", "Hello World");
assertMockEndpointsSatisfied();
// additional test to ensure correct endpoints in registry
assertNotNull(context.hasEndpoint("direct:start"));
assertNotNull(context.hasEndpoint("direct:foo"));
assertNotNull(context.hasEndpoint("log:foo"));
assertNotNull(context.hasEndpoint("mock:result"));
// all the endpoints was mocked
assertNotNull(context.hasEndpoint("mock:direct:start"));
assertNotNull(context.hasEndpoint("mock:direct:foo"));
assertNotNull(context.hasEndpoint("mock:log:foo"));
}
请注意,模拟端点被授予 URI mock:<endpoint>,如 mock:direct:foo。在 INFO 级别获取正在模拟的端点的 Camel 日志:
INFO Adviced endpoint [direct://foo] with mock endpoint [mock:direct:foo]
模拟的端点是没有参数
端点,这些端点将将其参数剥离。例如,端点 log:foo?showAll=true 将被模拟到以下端点 mock:log:foo。注意参数已被删除。
也可以使用某一模式来模拟某些端点。例如,要模拟您执行的所有日志端点,如下所示:
建议 只使用模式模拟日志端点
public void testAdvisedMockEndpointsWithPattern() throws Exception {
// advice the first route using the inlined AdviceWith route builder
// which has extended capabilities than the regular route builder
context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
// mock only log endpoints
mockEndpoints("log*");
}
});
// now we can refer to log:foo as a mock and set our expectations
getMockEndpoint("mock:log:foo").expectedBodiesReceived("Bye World");
getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
template.sendBody("direct:start", "Hello World");
assertMockEndpointsSatisfied();
// additional test to ensure correct endpoints in registry
assertNotNull(context.hasEndpoint("direct:start"));
assertNotNull(context.hasEndpoint("direct:foo"));
assertNotNull(context.hasEndpoint("log:foo"));
assertNotNull(context.hasEndpoint("mock:result"));
// only the log:foo endpoint was mocked
assertNotNull(context.hasEndpoint("mock:log:foo"));
assertNull(context.hasEndpoint("mock:direct:start"));
assertNull(context.hasEndpoint("mock:direct:foo"));
}
支持的模式可以是通配符或正则表达式。在 Intercept 它与 Camel 使用的匹配功能相同时,请参阅该详情。
请记住,模拟端点会导致在消息到达模拟时复制消息。
这意味着 Camel 将使用更多内存。当您以大量消息发送时,这可能不适合。