87.12. XML DSL を使用した既存のエンドポイントのモック


上記のように単体テストに camel-test コンポーネントを使用しない場合は、ルートに XML ファイルを使用するときに別のアプローチを使用できます。
解決策は、単体テストで使用される新しい XML ファイルを作成し、テストするルートを含む目的の XML ファイルを含めることです。

camel-route.xml ファイルにルートがあるとします。

camel-route.xml

    <!-- this camel route is in the camel-route.xml file -->
    <camelContext xmlns="http://camel.apache.org/schema/spring">

        <route>
            <from uri="direct:start"/>
            <to uri="direct:foo"/>
            <to uri="log:foo"/>
            <to uri="mock:result"/>
        </route>

        <route>
            <from uri="direct:foo"/>
            <transform>
                <constant>Bye World</constant>
            </transform>
        </route>

    </camelContext>
Copy to Clipboard Toggle word wrap

次に、次のように新しい XML ファイルを作成します。このファイルには camel-route.xml ファイルが含まれ、Camel にすべてのエンドポイントをモックするように指示するクラス org.apache.camel.impl.InterceptSendToMockEndpointStrategy で Spring Bean を定義します。

test-camel-route.xml

    <!-- the Camel route is defined in another XML file -->
    <import resource="camel-route.xml"/>

    <!-- bean which enables mocking all endpoints -->
    <bean id="mockAllEndpoints" class="org.apache.camel.component.mock.InterceptSendToMockEndpointStrategy"/>
Copy to Clipboard Toggle word wrap

次に、ユニットテストで、camel-route.xml の代わりに新しい XML ファイル (test-camel-route.xml) を読み込みます。

すべての Log エンドポイントのみをモックするには、Bean のコンストラクターでパターンを定義できます。

<bean id="mockAllEndpoints" class="org.apache.camel.impl.InterceptSendToMockEndpointStrategy">
    <constructor-arg index="0" value="log*"/>
</bean>
Copy to Clipboard Toggle word wrap
トップに戻る
Red Hat logoGithubredditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

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

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

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

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

会社概要

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

Theme

© 2025 Red Hat