Let's say I have my routes created in separate RouteBuilder class. It looks like:
I'd like to unit test this route with no JMS broker and no DB. I know I can mock my Processor implementations but that's not enough. I don't want to change this route (let's suppose I got that class in jar file). As far as I know from Camel in Action (sec. 6.2.6), to be able to use mocks of endpoints and other stuff I need to change my route endpoint definitions (in book's example this is change of "mina:tcp://miranda" to "mock:miranda" etc).
Is it possible to test the flow in complete isolation without changing route definitions? If I got my RouteBuilder as a separate class, am I forced to somehow "copy" route definition and change it manually? Isn't it testing the wrong thing?
I'm quite new to Camel and for me it'd be really cool to be able to have isolated unit test while deveoping routes. Just to be able to change something, run small test, observe result and so on.
As a possible solution, you can set the URI for your REST route and use that URI in your junit test. In order to do so, you need to switch RestDefinition to RouteDefinition by calling route method, and then you can call the from method and set the uri argument. Example using direct endpoint: rest("/ordernumber").
The CamelContext is the runtime system, which holds everything together as depicted in the figure below. The CamelContext provides access to many useful services, the most notable being components, type converters, a registry, endpoints, routes, data formats, and languages.
A route in Apache Camel is a sequence of steps, executed in order by Camel, that consume and process a message. A Camel route starts with a consumer, and is followed by a chain of endpoints and processors. So firstly, a route receives a message, using a consumer – perhaps from a file on disk, or a message queue.
I have
<bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent"> <property name="location" value="classpath:shop.properties"/> </bean> <route> <from uri="direct://stock"/> <to uri="{{stock.out}}"/> </route>
in my spring file and then in the shop.properties on the test class path i have a stock.out=xxxx which is replaced at runtime so i can have to different routes one for runtime and one for test
theres a better example in 6.1.6 unit testing in multiple environments
Assuming the RouteBuilder class has hardcoded endpoints then its a bit tougher to test. However if the RouteBuilder using the property placeholder for endpoint uris, then you often will be able to use a different set of endpoint uris for unit tests. As explained in chapter 6 of the Camel book.
If they are hardcoded then you can use the advice with feature in your unit test as shown here: https://camel.apache.org/components/latest/others/test-cdi.html#CDITesting-RoutesadvisingwithadviceWith
In Camel 2.7 we made it possible to manipulate the route much easier, so you can remove parts, replace parts, etc. Thats the weaving stuff that link talks about.
For example to simulate sending a message to a database endpoint, you can use that above and replace the to with another where you send it to a mock instead.
In previous releases you can use the interceptSendToEndpoint trick, which is also covered in the Camel book (section 6.3.3)
Oh you can also replace components with mock component as shown on page 169. Now in Camel 2.8 onwards the mock component will no longer complain about uri parameters it doesnt know. That means its much easier to replace components with mocks on a per component level.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With