37.11. 使用 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 文件,并定义一个带有类 org.apache.camel.impl.InterceptSendToMockEndpointStrategy 的 spring bean,它告知 Camel 模拟所有端点:

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

然后,在单元测试中载入新的 XML 文件(test-camel-route.xml),而不是 camel-route.xml

要只模拟 所有日志 端点,您可以在构建器中定义 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

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat