44.9. 既存のエンドポイントのモック
Camel では、Camel ルートで既存のエンドポイントを自動的にモックできるようになりました。
使い方
エンドポイントはまだ動作中です。異なるのは、Mock エンドポイントが注入され、最初にメッセージを受信してから、メッセージをターゲットエンドポイントに委任する点です。これは、一種のインターセプトおよびデリゲートまたはエンドポイントリスナーと見なすことができます。
以下の特定のルートがあるとします。
ルート
@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 の adviceWith 機能を使用して、単体テストから特定のルートのすべてのエンドポイントをモックできます。
adviceWith すべてのエンドポイントをモックする
@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"));
}
モックエンドポイントには、mock:direct:foo などの URI mock:<endpoint> が指定されていることに注意してください。Camel は、モックされているエンドポイントを INFO レベルでログに記録します。
INFO Adviced endpoint [direct://foo] with mock endpoint [mock:direct:foo]
モックされたエンドポイントにはパラメーターがありません
モックされたエンドポイントは、パラメーターが取り除かれます。たとえば、エンドポイント log:foo?showAll=true は、次のエンドポイント mock:log:foo にモックされます。パラメーターが削除されていることに注意してください。
パターンを使用して特定のエンドポイントのみをモックすることもできます。たとえば、すべての log エンドポイントをモックするには、次のようにします。
adviceWith パターンを使用してログエンドポイントのみをモックする
@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 で使用されるのと同じマッチング関数として、Intercept で詳細を参照してください。
エンドポイントをモックすると、メッセージがモックに到着したときにメッセージがコピーされることに注意してください。
つまり、Camel はより多くのメモリーを使用します。大量のメッセージを送信する場合、これは適切ではない場合があります。