I need to write some unit tests for Camel routes that use CXF component to call a SOAP web service.
What would be the cleanest way to skip the SOAP request and return a stubbed response?
I'm trying with mock endpoints but the request is either passed to the real endpoint or ignored in whenAnyExchangeReceived
processor callback.
I'm configuring Camel with Blueprint and running it with camel-test-blueprint 2.17.1. This is how my blueprint.xml configuration looks like.
<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>
Test class
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
}
}