Search code examples
apache-cameljbossfuseblueprint-osgicamel-testcamel-blueprint

Mocking OSGi reference in CamelBlueprintTest does not work


I'm currently setting up tests with CamelBlueprintTestSupport.

In my blueprint.xml, I tried the following implementation:

<reference id="foo" interface="com.myexample.FooInterface" />

Mocking FooInterface in my test class with

protected void addServicesOnStartup(Map<String, KeyValueHolder<Object, Dictionary>> services) { 
        FooInterface foo = Mockito.mock(FooImpl.class);     
        services.put(FooInterface.class.getCanonicalName(), asService(foo, null));      
        super.addServicesOnStartup(services);       
}

works fine (where FooImpl implements FooInterface, of course), e.g. executing the test (the test simply contains assert(true) cause I'd only like to check the test configuration) ends up positive.

But in my real implementation, I do not have an interface as service. Instead, it is a class referenced this way:

<bean class=com.myexample.FooBar" id="foobar">
        <property name="foo" ref="foo" />
    </bean>

    <bean class="com.myexample.FooImpl" id="foo"/>

  <reference id="fooBarReference"
        component-name="foobar"
        interface="com.myexample.FooImpl" ext:proxy-method="classes" />

My mock configuration in the test is this one:

protected void addServicesOnStartup(Map<String, KeyValueHolder<Object, Dictionary>> services) { 
    FooImpl foo = Mockito.mock(FooImpl.class);      
    services.put(FooImpl.class.getCanonicalName(), asService(foo, null));    
    super.addServicesOnStartup(services);       
} 

Executing the test now fails with the following exception:

java.lang.RuntimeException: Gave up waiting for BlueprintContainer from bundle "FooTest" at com.myexample.test.FooTest.setUp(FooTest.java:49)

I cannot see what's actually wrong. By the way, the implementation of the camel route on Karaf runs without any problems. I don't know if my test set up is wrong or if it's a bug in the CamelBlueprintTestSupport.


Solution

  • Finally, there is a solution:

    Properties dictionary = new Properties();
    dictionary.setProperty("osgi.service.blueprint.compname","foobar"); 
    services.put(FooImpl.class.getName(), asService(new FooImpl(), dictionary));
    

    CU, iE