0
votes

I'm guessing something is wrong integrating my spring and camel contexts.

I'm unit testing a Camel Component, and I'm trying to inject a bean (MainframeEncoderProvider) generated by spring component-scan. I can see that the bean is being constructed (breakpointing an init block), and I can autowire it into ToFalinkProducerTest, but it's not getting into the camel Component. The component is instantiated via META-INF auto-discovery, if that's relevant (http://camel.apache.org/how-do-i-add-a-component.html)

Test class setup:

@RunWith(CamelSpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:spring-config/testContext.xml"})
public class ToFalinkProducerTest extends CamelTestSupport{

[some tests...]

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        public void configure() {
            //@formatter:off

            from("direct:start")
                    .to("tofalink:x?encoderName=test")
                    .to("mock:result");
            //@formatter:on
        }
    };
}

testContext:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd

        http://camel.apache.org/schema/spring
        http://camel.apache.org/schema/spring/camel-spring.xsd
        ">

    <!-- lightweight testcontext -->
    <context:annotation-config/>
    <context:component-scan base-package="net.mo"/>

    <camel:camelContext>
            <camel:contextScan/>
    </camel:camelContext>


</beans>

component:

/**
 * Represents the component that manages {@link ToFalinkEndpoint}.
 */
public class ToFalinkComponent extends DefaultComponent {

    @Autowired
    private MainframeEncoderProvider provider;

    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
        Endpoint endpoint = new ToFalinkEndpoint(uri, this);
        setProperties(endpoint, parameters);
        return endpoint;
    }

    protected MainframeEncoderProvider getProvider() {
        return this.provider;
    }
}
1
is this ToFalinkComponent a spring bean? - pvpkiran
Not sure. See my comment on the auto-discovery setup, I don't know if that qualifies or is a separate process. - NielsR
If I remove the auto-discovery for this component, and annotate ToFalinkComponent with @Component(value="tofalink") so Spring builds it, the provider is still not Autowired, and Camel can't find the component to create the Endpoint. - NielsR

1 Answers

0
votes

Managed to fix this, after a lot of faffing around. Replaced CamelTestSupport extension with CamelSpringTestSupport, and dropped @RunWith and @ContextConfiguration. Implemented createApplicationContext() with my xml spring context file, to retain the component scanning.

left the auto-discovery mechanic in place.

Now I just have to see whether it works at runtime :/