Question

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).

Was it helpful?

Solution

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
}

OTHER TIPS

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.

The answer provided by @Matthew Wilson is no longer recommended starting with Camel 3.0.0

His solution is still in the ballpark but the implementation details have changed. I have chosen to inject it in setUp (the example is in Kotlin, use your IDE hints to produce the same in Java):

override fun setUp() {
    super.setUp()
    context.registry.bind("yourBean", YourBean())
}

As you can see, the registry is still involved but now you can only get it from the context. I consider it cleaner to keep these kinds of setup routines in the conveniently named overrideable method setUp. Just don't forget to call the parent version.

If there is a better place to put this kind of routines in, let me know so I can upgrade the answer.

Docs: https://camel.apache.org/manual/latest/camel-3-migration-guide.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top