5

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

Community
  • 1
  • 1
dermoritz
  • 12,519
  • 25
  • 97
  • 185

3 Answers3

11

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
}
Matthew Wilson
  • 2,045
  • 14
  • 27
  • 1
    if i want to use the same registry in test as in my normal code how to reuse the registry? i don't want to copy paste the code - if it changes the test will fail. Is there a way to give (or get/set) the main a JndiRegistry? – dermoritz Apr 22 '14 at 15:10
  • the hack I used was to add hook into the doPostSetup() method the test harness provides to call the method used to setup the registry in the CamelContextListener class for the prod context. a bit of boilerplate plumbing to be sure but it's not much and it ensures both prod and test have the same registry. – snerd Feb 19 '19 at 19:54
  • I guess I can only edit a comment for 5m (??) so posting better phrasing. where OP has '//use jndi.bind' you can directly call the method in your CamelContextLifecycle class that handles the registry for prod. the camel context seems to be set up at this point so it's pretty easy to get both prod and test contexts to use the same registry population code – snerd Feb 19 '19 at 20:06
  • Your answer is no longer recommended as of Camel 3.0.0 due to some things being deprecated. Check out my answer if you are migrating – hanzo2001 Jan 20 '20 at 13:51
3

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.

Willem Jiang
  • 3,291
  • 1
  • 14
  • 14
1

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

hanzo2001
  • 1,338
  • 1
  • 10
  • 24