Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unit test a route with a bean which will access DB?

Here DB is just an example. It means something can't be prepared in unit test environment.

Consider the route below:

DBBean dbBean = new DBBean();
from("direct:test").bean(dbBean).to("direct:someOtherLogic");

When do unit test, is there any approach to mock the 'dbBean'? In unit test, it's hard to set up a real DB.

Thanks for your help.

like image 453
Javen Avatar asked Jan 19 '12 01:01

Javen


1 Answers

Camel has a test kit, which allows you to manipulate the route before being tested. Then you can keep the route untouched, and then replace parts of the route, and whatnot. Its a bit elaborate, and its documented as the advice-with functionality here: http://camel.apache.org/advicewith.html

It generally works easier if the EIPs have IDs assigned, as you can then refer to those ids, and replace it with something else.

However if you know that you want to replace the first BeanDefinition you can do:

weaveByType(BeanDefinition.class).selectFirst().replace().to("mock:dbBean");

See the above links how to use this with the advice-with in the Camel Test Kit (eg camel-test) JAR.

Mind that its recommended to tell Camel Test Kit that you are using advice with, which you do as documented in the bottom of that link.

like image 197
Claus Ibsen Avatar answered Nov 09 '22 22:11

Claus Ibsen