Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I register beans in camel unit tests that use beans?

I want to unit test single routes configured in java that uses beans. I read in camel in action (chapter 6.1.4) how to do this:

protected RouteBuilder createRouteBuilder() throws Exception {
    return new myRoute();
}

But in my case the rout needs some beans to be registered. I know how to register beans in standalone app: see here But how to register beans within "CamelTestSupport"? Is there a way to use beans without registry? Probably by injecting them (all beans hav no arg constructors)? I am using Guice and in my tests i am using Jukito (Guice+Mockito).

like image 478
dermoritz Avatar asked Apr 17 '14 13:04

dermoritz


2 Answers

Afer Camel 3.0.0

You can now update the JNDI registry from anywhere you have access to the camel context.

context.getRegistry().bind("myId", myBean);

More info available here https://camel.apache.org/manual/latest/camel-3-migration-guide.html#_camel_test

Before Camel 3.0.0

You need to override the createRegistry() method,

@Override
protected JndiRegistry createRegistry() throws Exception {
    JndiRegistry jndi = super.createRegistry();

    //use jndi.bind to bind your beans

    return jndi;
}

@Test
public void test() {
    //perform test
}
like image 185
Matthew Wilson Avatar answered Oct 19 '22 20:10

Matthew Wilson


No, you cannot use beans without registry.

You need to use the registry to hold the beans instance, otherwise Camel cannot look up the bean for you. You just need to override the createRegistry() method to setup right registry with your beans if your test class extends CamelTestSupport.

like image 36
Willem Jiang Avatar answered Oct 19 '22 19:10

Willem Jiang