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


Camel では、Camel ルートで既存のエンドポイントを自動的にモックできるようになりました。

注記

使い方
エンドポイントはまだ動作中です。異なるのは、モック エンドポイントが挿入され、最初にメッセージを受信してから、メッセージをターゲットエンドポイントに委任することです。これは、一種のインターセプトおよびデリゲートまたはエンドポイントリスナーと見なすことができます。

以下の特定のルートがあるとします。

ルート

    @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"));
            }
        };
    }
Copy to Clipboard Toggle word wrap

次に示すように、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"));
    }
Copy to Clipboard Toggle word wrap

モックエンドポイントには、mock:direct:foo などの URI mock:<endpoint> が指定されていることに注意してください。Camel は、モックされているエンドポイントを INFO レベルでログに記録します。

INFO  Adviced endpoint [direct://foo] with mock endpoint [mock:direct:foo]
Copy to Clipboard Toggle word wrap
注記

モックされたエンドポイントにはパラメーターがありません
モックされたエンドポイントは、パラメーターが取り除かれます。たとえば、エンドポイント 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"));
    }
Copy to Clipboard Toggle word wrap

サポートされるパターンは、ワイルドカードまたは正規表現です。Camel で使用されるのと同じマッチング関数として、Intercept で詳細を参照してください。

注記

エンドポイントをモックすると、メッセージがモックに到着したときにメッセージがコピーされることに注意してください。
つまり、Camel はより多くのメモリーを使用します。大量のメッセージを送信する場合、これは適切ではない場合があります。

トップに戻る
Red Hat logoGithubredditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

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

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

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

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

会社概要

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

Theme

© 2025 Red Hat