226.7. 既存のエンドポイントのモック
Camel 2.7 以降で利用可能
Camel では、Camel ルートで既存のエンドポイントを自動的にモックできるようになりました。
使い方 エンドポイントはまだ動作中です。異なるのは、Mock エンドポイントが注入され、最初にメッセージを受信してから、メッセージをターゲットエンドポイントに委任する点です。これは、一種のインターセプトおよびデリゲートまたはエンドポイントリスナーと見なすことができます。
以下の特定のルートがあるとします。
ルート
@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 機能を使用して、単体テストから特定のルートのすべてのエンドポイントをモックできます。
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"));
}
モックエンドポイントには、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 パターンを使用してログエンドポイントのみをモックする
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"));
}
サポートされるパターンは、ワイルドカードまたは正規表現です。Camel で使用されるのと同じマッチング関数として、Intercept で詳細を参照してください。
エンドポイントをモックすると、メッセージがモックに到着したときにメッセージがコピーされることに注意してください。
つまり、Camel はより多くのメモリーを使用します。大量のメッセージを送信する場合、これは適切ではない場合があります。