Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how start a route in a Camel test with mocked Endpoints

I'm starting out with Camel and I'm having some problems writing a test. My use case is exactly the same as the cfx proxy example. Except that I don't need the "RealWebservice". Now I'm trying to write a unit test (not an integration test as included with the example), using the annotation approach:

@RunWith(CamelSpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:application-context.xml" })
@MockEndpointsAndSkip
public class RoutesTest {

@Autowired
CamelContext camelContext;

@EndpointInject(uri = "mock:cxf:bean:cxfEndpoint", context = "camelContext")
MockEndpoint cxfEndpoint;

@EndpointInject(uri = "mock:log:input", context = "camelContext")
MockEndpoint logInputEndpoint;

@EndpointInject(uri = "mock:http:realhostname:8211/service", context = "camelContext")
MockEndpoint realEndpoint;

@EndpointInject(uri = "mock:cxf:bean:cxfEndpoint")
ProducerTemplate producer;

@Test
public void testLeleuxMifidRoute() throws InterruptedException {
    String body = "<blah/>";

    cxfEndpoint.expectedBodiesReceived(body);
    logInputEndpoint.expectedBodiesReceived(body);
    realEndpoint.expectedBodiesReceived(body);

    producer.sendBody(body);

    MockEndpoint.assertIsSatisfied(camelContext);
}
}

The cxfEndpoint receives the message but the other endpoints don't.

The route looks like this (it works when I run it and send a message with SoapUI, obviously I obfuscated the ips and beannames in this example):

<endpoint id="callRealWebService" uri="http://realhostname:8211/service?throwExceptionOnFailure=true" /> 
<route>
  <from uri="cxf:bean:cxfEndpoint?dataFormat=MESSAGE"/>
  <to uri="log:input?showStreams=true"/>
  <to ref="callRealWebService"/>
  <to uri="log:output"/>
</route>

What am I doing wrong? All the examples and other questions I found seem to use "direct:start" or change the production route.

like image 222
Nicolas Mommaerts Avatar asked Jan 15 '13 11:01

Nicolas Mommaerts


1 Answers

One approach that we used with success is to have different properties files for test execution and for the main code.

We define, inside the camel-context, the property

<propertyPlaceholder id="properties"
            location="classpath:META-INF/uri.properties" xmlns="http://camel.apache.org/schema/spring" />

In the folder /src/main/resources/META-INF/ we have the uri.properties file for the main code and /src/test/resources/META-INF/ we have the uri.properties for the test execution.

Your route has to be rewritten using property placeholder instead of the real uri values, using the notation {{properties.name}}

<route>
  <from uri="{{cxf.bean.cxfEndpoint}}"/>
</route>

the main uri.properties will be

cxf.bean.cxfEndpoint=cxf:bean:cxfEndpoint?dataFormat=MESSAGE

the test uri.properties will be

cxf.bean.cxfEndpoint=direct:start

Using this configuration, you'll be able to test your route easily.

like image 175
MPavesi Avatar answered Oct 22 '22 11:10

MPavesi