検索

226.7. 既存のエンドポイントのモック

download PDF

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 はより多くのメモリーを使用します。大量のメッセージを送信する場合、これは適切ではない場合があります。

Red Hat logoGithubRedditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

© 2024 Red Hat, Inc.