37.11. 使用 XML DSL 模拟现有端点
如果不使用 camel-test
组件进行单元测试(如上所示),您可以在将 XML 文件用于路由时使用不同的方法。
解决方案是创建新 XML 文件,供单元测试使用,然后包含包含您要测试的路由的预期 XML 文件。
假设我们在 camel-route.xml
文件中有路由:
camel-route.xml
然后,我们创建一个新的 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"/>
<!-- 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"/>
然后,在您的单元测试中载入新的 XML 文件(test-camel-route.xml
)而不是 camel-route.xml
。
要只模拟所有日志端点,您可以在 bean 的构造器中定义模式: https://access.redhat.com/documentation/zh-cn/red_hat_integration/2022.q4/html-single/camel_spring_boot_reference/index#csb-camel-log-component-starter
<bean id="mockAllEndpoints" class="org.apache.camel.impl.InterceptSendToMockEndpointStrategy"> <constructor-arg index="0" value="log*"/> </bean>
<bean id="mockAllEndpoints" class="org.apache.camel.impl.InterceptSendToMockEndpointStrategy">
<constructor-arg index="0" value="log*"/>
</bean>