How to start a route in Camel test with mocked endpoints

I start with Camel and am having trouble writing a test. My usage example is the same as cfx proxy example. Except that I don't need a "RealWebservice". Now I'm trying to write unit test (not the integration test included in 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); } } 

CxfEndpoint receives the message , but other endpoints do not have.

The route looks like this (it works when I run it and send a message with SoapUI, obviously I obfuscated 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 I gave and other questions seem to use "direct: start" or change the production route.

+8
java apache-camel cxf
source share
1 answer

One of the approaches that we have used with success is the availability of different property files for the test and for the main code.

In the context of a camel, we define a 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 uri.properties for the test.

Your route should be rewritten using the property placeholder instead of the real uri values โ€‹โ€‹using the notation {{properties.name}}

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

core uri.properties will be

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

uri.properties test will be

 cxf.bean.cxfEndpoint=direct:start 

Using this configuration, you can easily test your route.

+4
source share

All Articles