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

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

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

camel-route.xml 1

<!-- 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>

次に、次のように新しい 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"/>

次に、ユニットテストで、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>