Search code examples
javaspringtestingapache-camelapache-servicemix

How to set property values during testing for CamelSpringTestSupport based tests


Im creating a ServiceMix module that consists of a Camel route.

In my beans.xml, I have:

<osgix:cm-properties id="companyProps"
    persistent-id="com.company.integration">
</osgix:cm-properties>

<ctx:property-placeholder location="
    file:${karaf.base}/etc/com.company.integration.cfg
" />

This means I can define other items using properties from the file, like:

<http-conf:conduit name="*.http-conduit">
    <http-conf:authorization>
        <security:UserName>${username}</security:UserName>
        <security:Password>${password}</security:Password>
    </http-conf:authorization>
</http-conf:conduit>

I can also access the properties in my java classes if I create a bean and inject it:

<bean id="myConfig" class="com.company.integration.MyConfig">
    <osgix:managed-properties persistent-id="com.company.integration" />
</bean>

The problem is when Im writing my unit tests. Currently Im using a copy of my beans.xml with test values, but of course I'd like to use the real beans.xml and supply values for the properties.

public class myTest extends CamelSpringTestSupport
{
@Override
protected AbstractXmlApplicationContext createApplicationContext()
{
    return new ClassPathXmlApplicationContext(new String[] {
            "/META-INF/spring/beans.xml"
        ,   "/META-INF/spring/test.xml"
    });

}   
}

I'd like to get rid of test.xml and preferably load the test-properties from a property file. I've seen some references to PropertyComponent, but Im unable to get this to work :-(


Solution

  • In our organization, we separate our spring-osgi configuration from our spring bean configuration.

    In our spring-osgi configuration we would have the following:

    <osgix:cm-properties id="companyProps" persistent-id="com.company.integration">
        <prop key="name">value</prop>
        <prop key="name">value</prop>
        ...
    </osgix:cm-properties>
    

    as well as any osgi-specific configuration (such as service registration, events, etc.)

    In our spring-bean configuration, we define all the beans that we COULD use outside of an osgi container, including camel routes/context etc.

    For our testing, we start a ClassPathXmlContext using our spring bean definition and a test spring configuration that contains the following:

    <ctx:property-placeholder location="classpath*:test.properties" />
    

    As well as any mocks that we need for the osgi specific beans.

    Hope this helps.