Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
657 views
in Technique[技术] by (71.8m points)

junit - How to unit test production routes in Apache Camel?

Let's say I have my routes created in separate RouteBuilder class. It looks like:

  • grab message from JMS queue
  • do some transformation, validation etc
  • depending on validation results forward to specific JMS queue and save something in DB

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...