dimanche 3 janvier 2016

Camel @BeanInject doesn't work in Blueprint-Test

I'm trying to use CamelBlueprintTestSupport in order to test a camel route applying a number of beans. In the production code the beans are defined in a separate blueprint XML. In the test class I overwrite the addServicesOnStartup method just like documented. My unit test class looks like this:

public class MyUnit_Test extends CamelBlueprintTestSupport {

  @Override
  protected String getBlueprintDescriptor() {
    return "/OSGI-INF/blueprint/camel-routes.xml";
  }

  @Override
  protected void addServicesOnStartup(Map<String, KeyValueHolder<Object, Dictionary>> services) {    
    services.put("beanA", asService(new BeanA(), null, null));
    services.put("beanB", asService(new BeanB(), null, null));
  }

  @EndpointInject(uri = "mock:endpointMock")
  private MockEndpoint endpointMoc;

  @Test
  public void test_myRoute() throws Exception {
    (test code)
  }
}

One bean (BeanA) is referenced in the route:

<camelContext>
  <route>
    ...
    <bean ref="beanA" method="myMethod" />
    ...
  </route>
</camelContext>

This one needs access to another bean (BeanB). I use the @BeanInject annotation inide BeanA for this:

public class BeanA {

  @BeanInject("beanB")
  private BeanB beanB;

  public void myMethod(Exchange exchange) {
    beanB.getSomething();
  ...
}

In production the dependency injection works well.

Problem in unit test

In the unit test BeanA can be referenced by the route. But the injection of BeanB inside BeanA seems not to work. I receive NPE in BeanA.myMethod() when the code tries to access beanB.

It seems that the dependency injection only works for the routes in the blueprint XML but not recursively for the injected beans themselves.

Attempts

  • I tried to overwrite CamelTestSupport's createRegistry method and to add my beans by this way. But it didn't work. In fact I found that createRegistry isn't called in CamelBlueprintTestSupport startup anyway.

  • In the Camel documentation for Blueprint testing they refer to PojoSR as registry framework. But I could not find any more examples or directions how to use this for my needs.

Aucun commentaire:

Enregistrer un commentaire