93.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>
然后,我们按如下方式创建新的 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"/>
然后,在单元测试中,您将加载新的 XML 文件(test-camel-route.xml
)而不是 camel-route.xml
。
要只 mock 所有 Log 端点,您可以在 bean 的构造器中定义模式:
<bean id="mockAllEndpoints" class="org.apache.camel.impl.InterceptSendToMockEndpointStrategy"> <constructor-arg index="0" value="log*"/> </bean>