1
votes

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
    }
}
1
This is exactly the problem we're facing. For the moment we did not see any other alternative to create a fake server (like with SOAP-UI) but we did not go this way. For the moment we make integration testing only for this case with the real WS.рüффп
@ruffp I posted my actual setup. Works pretty well for my case, hope you can adapt it to your situation.Alessandro Da Rugna

1 Answers

0
votes

I'm posting my solution. I totally removed CXF bean from my route and replaced its endpoint with a property. During tests the property value is changed to an appropriate endpoint e.g. direct:something.
My setup is a bit more complicate as it involves dynamic routers but here's the gist of it:

<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>

Default value is ok for production use, and it is overridden only during testing by defining cxfEndpoint inside the appropriate my.app.cfg file.