Sunday, 24 July 2016

Testing Apache Camel routes

Running Apache Camel on top of Spring is quite popular nowadays and there is a multitude of resources on how to create the routes. However, sooner or later you will want to test the routes and this is where people usually hit a brick wall, because the documentation is a bit confusing. If you're still trying to find a good way to test your routes, then read on.

A simple route

We are using 3 queues: seda:groceries, seda:fruits and seda:vegetables. In case you're wondering what SEDA is, it's just an in-memory queue provided by Camel and it can be replaced by anything else like JMS or RabbitMQ, the principle stays the same.
The type of objects pushed into the groceries queue is checked via Camel's choice() construct and the fruits are redirected into the fruits queue, while the vegetables are redirected into the vegetables queue. The fruits queue is processed by FruitProcessor and the vegetable queue is processed by VegetableProcessor.

The test setup

In order to test the routes, we need to start Camel, which is not possible using the production configuration, considering the fact that Camel will connect to various third party services. Also, we need to make sure that we don't run the real processors, because we only want to test the routes.

This is the test setup, which is actually the complicated part of all this:

A few essential points for the test setup:
  1. We are using CamelSpringJUnit4ClassRunner to run the tests
  2. The test has its own configuration, defined in the ContextConfig inner class, which extends SingleRouteCamelConfiguration
  3. The route() method in ContextConfig() defines the route builder class that we are testing
  4. All the processors are defined as beans and they are mocked by using Camel's MockEndpoint. That allows us to replace the actual processors with mocks and make assertions on how the messages are travelling through the routes.
  5. The mock processors are autowired into the test class so that assertions can be made against them

The tests

After the setup is complete, the actual tests are quite straightforward:
  1. When I push a Fruit object into the groceries queue, I would like the FruitProcessor to process the message
  2. When I push a Vegetable object into the groceries queue, I would like the VegetableProcessor to process the message
This is the actual code:
There are many ways to go about testing the routes, but the setup and test strategy remain the same. I hope you found this useful, if you did then please share. The maven project is available here.

Share on Twitter | Share on Facebook | Share on Google+


  1. I regard something truly interesting about your web blog so I saved to favorites .
    Packers And Movers in Bangalore

  2. It has always been my belief that very good writing such as this takes researching and also talent. It’s very apparent you have performed your homework.Good job!Also visit my web blog …
    Packers And Movers Bangalore

  3. I must be thankful for sharing your ideas.Thank you for the good writeup.its a brellint job.please visit here for more detail:
    Packers and Movers Delhi

  4. An excellent information provided thanks for all the information i must say great efforts made by you.
    thanks a lot for all the information you provided.
    please visit our official website for further details-
    Packers And Movers Gurgaon
    Packers And Movers In Gurgaon

  5. Thank you for sharing. please visit here for more detail:

    Packers and Movers Delhi

  6. Thank you for sharing.please visit here for more detail:

    Packers and Movers Hyderabad


  7. شركة تنظيف فلل بالمدينة المنورة المنورة 0542742095 -0509178637
    | المدينة المنورة
    شركة تنظيف فلل بالمدينة المنورة

    يمكن الآن القيام بعملية التنظيف للفلل بغاية في السهولة الفلل معروفة بمساحتها الكبيرة وطوابقها المتعددة ولا يمكن لأصحابها أن يقوموا بعملية التنظيف كتنظيف وغسيل السجاد الموجود بها وأيضا الموكيت الذي يغطي معظم أرضيات المكان وأيضا تنظيف موقف السيارات الموجود بها والحديقة ولكن من خلال شركة تنظيف فلل بالمدينة المنورة يتم توفير العديد من الخدمات ويمكن طلبها مرة واحدة أو كل خدمة على حدى ومن ضمن تلك الخدمات لو كنت تريد تنظيف الممشى الخارجي للفلل فيمكن الاتصال وطلب خدمة جلى الأرضيات وأيضا يوجد لدينا خدمة تنظيف واجهات الفلل بأحدث الطرق حيث يتم استخدام مواد تقوم بالحفاظ على ألوان البياض وحمايته من العوامل الجوية كالأمطار والأتربة من الرياح الموسمية وأيضا خدمة تنظيف كنب وموكيت وستائر

    للتواصل معانا
    0542742095 -0509178637

    او زيارة موقعنا
    كما نقدم اليكم المزيد من الخدمات
    شركة تنظيف منازل بالمدينة المنورة

    شركة تنظيف شقق بالمدينة المنورة

    شركة تنظيف عمائر بالمدينة المنورة
    شركة تنظيف بالمدينة المنورة

  8. Thanks for such avant-garde and amazing information..Good actuality is absolutely affable information.
    Web Designing Company in Bangalore, Web Development Company Bangalore