101.10. 模拟现有端点
Camel 现在允许您自动模拟 Camel 路由中的现有端点。
它的工作原理
端点仍在操作中。发生了什么情况是,Mock 端点被注入并首先接收消息,然后将消息委派给目标端点。您可以将它视为拦截器类型,以及委派或端点监听程序。
假设您有以下给定路由:
Route
@Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start").routeId("start") .to("direct:foo").to("log:foo").to("mock:result"); from("direct:foo").routeId("foo") .transform(constant("Bye World")); } }; }
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").routeId("start")
.to("direct:foo").to("log:foo").to("mock:result");
from("direct:foo").routeId("foo")
.transform(constant("Bye World"));
}
};
}
然后,您可以使用 Camel 中的 recommendations With
功能模拟单元测试中给定路由中的所有端点,如下所示:
recommendationsWith
模拟所有端点
@Test public void testAdvisedMockEndpoints() throws Exception { // advice the start route using the inlined AdviceWith lambda style route builder // which has extended capabilities than the regular route builder AdviceWith.adviceWith(context, "start", a -> // mock all endpoints a.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")); }
@Test
public void testAdvisedMockEndpoints() throws Exception {
// advice the start route using the inlined AdviceWith lambda style route builder
// which has extended capabilities than the regular route builder
AdviceWith.adviceWith(context, "start", a ->
// mock all endpoints
a.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]
INFO Adviced endpoint [direct://foo] with mock endpoint [mock:direct:foo]
模拟的端点没有参数
端点,被模拟将具有被模拟的参数被剥离。例如,端点 log:foo?showAll=true
将被模拟到以下端点 mock:log:foo
。注意参数已被删除。
也可以仅使用模式模拟某些端点。例如,要模拟您执行 的所有日志
端点,如下所示:
recommendationsWith
mocking only log endpoint using a pattern
@Test public void testAdvisedMockEndpointsWithPattern() throws Exception { // advice the start route using the inlined AdviceWith lambda style route builder // which has extended capabilities than the regular route builder AdviceWith.adviceWith(context, "start", a -> // mock only log endpoints a.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")); }
@Test
public void testAdvisedMockEndpointsWithPattern() throws Exception {
// advice the start route using the inlined AdviceWith lambda style route builder
// which has extended capabilities than the regular route builder
AdviceWith.adviceWith(context, "start", a ->
// mock only log endpoints
a.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"));
}
支持的模式可以是通配符或正则表达式。请参阅与 Camel 使用的相同匹配功能进行通信的更多详细信息。
请注意,模拟端点会在到达模拟时复制消息。
这意味着 Camel 将使用更多内存。当您发送大量消息时,这可能不适用。