我需要为使用CXF组件调用SOAP Web服务的Camel路由编写一些单元测试
跳过SOAP请求并返回存根响应的最简洁方法是什么?
我正在尝试使用模拟端点,但请求要么传递给真实端点,要么在whenAnyExchangeReceived
处理器回调中被忽略。
我正在使用Blueprint配置Camel并使用camel-test-blueprint 2.17.1运行它。这就是我的blueprint.xml配置的样子。
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
xmlns:camel="http://camel.apache.org/schema/blueprint"
xmlns:camelcxf="http://camel.apache.org/schema/blueprint/cxf"
xsi:schemaLocation="
http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.0.0.xsd
http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">
<cm:property-placeholder id="routeConfiguration"
persistent-id="my.app" />
<camelcxf:cxfEndpoint id="myWebService"
address="${myWebService.url}"
wsdlURL="classpath:wsdl/MyWebService.wsdl"
serviceClass="my.web.service.Ws"
serviceName="s:MyWebServiceImpl"
xmlns:s="http://mywebservice.it/"/>
<camelContext trace="false" id="myCamelContext"
xmlns="http://camel.apache.org/schema/blueprint">
<propertyPlaceholder location="blueprint:routeConfiguration" />
<route id="IwantToStubInsideHere">
<from uri="activemq:someQueue"/>
<to uri="cxf:bean:myWebService"/>
<to uri="direct:processWebServiceResponse"/>
</route>
</camelContext>
</blueprint>
测试课
public class RouteTest extends CamelBlueprintTestSupport {
@Override
public String isMockEndpointsAndSkip() {
return "cxf*";
}
@Test
public void testMethod() {
MockEndpoint mockEndpoint = getMockEndpoint("mock:cxf:bean:myWebService");
mockEndpoint.expectedMessageCount(1);
mockEndpoint.whenAnyExchangeReceived(new StubWsProcessor());
// run route here
}
}
答案 0 :(得分:0)
我正在发布我的解决方案。我完全从我的路线中删除了CXF bean并用一个属性替换了它的端点。在测试期间,属性值被更改为适当的端点,例如direct:something
。
我的设置有点复杂,因为它涉及动态路由器,但这是它的要点:
<cm:property-placeholder id="routeConfiguration"
persistent-id="my.app">
<cm:default-properties>
<cm:property name="cxfEndpoint" value="cxf:bean:myWebService"/>
</cm:default-properties>
</cm:property-placeholder>
.....
<route id="IwantToStubInsideHere">
<from uri="activemq:someQueue"/>
<to uri="${cxfEndpoint}"/>
<to uri="direct:processWebServiceResponse"/>
</route>
默认值可供生产使用,并且仅在测试期间通过在相应的 my.app.cfg 文件中定义cxfEndpoint
来覆盖它。