Flanfl
Flanfl

Reputation: 514

Camel processor unit/integration testing

I've probably completly missed something but I can't manage to test my route as I want to.

I've got the following bean :

@Component("fileProcessor")
public class FileProcessor {
   public boolean valid(@Header("customObject) CustomObject customObject,Exchange exchange) throws IOException{
       return false;
}

I've a route calling my bean like so :

from("direct:validationFile").routeId("validationFile").validate().method("fileProcessor","valid")
        // Other stuff
        .end();

Here is my unit test, based on a example I found:

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class})
@ContextConfiguration(locations = { "classpath:/tu-dao-beans.xml" })
public class FileProcessorTest extends CamelTestSupport {

    @EndpointInject(uri = "mock:result")
    protected MockEndpoint resultEndpoint;

    @Produce(uri = "direct:start")
    protected ProducerTemplate template;

    @Override
    public boolean isDumpRouteCoverage() {
        return true;
    }

    @Test
    public void testSendMatchingMessage() throws Exception {
        String expectedBody = "<matched/>";
        resultEndpoint.expectedBodiesReceived(expectedBody);
        template.sendBodyAndHeader(expectedBody, "foo", "bar");
        resultEndpoint.assertIsSatisfied();
    }

    @Test
    public void testSendNotMatchingMessage() throws Exception {
        resultEndpoint.expectedMessageCount(0);
        template.sendBodyAndHeader("<notMatched/>", "foo", "notMatchedHeaderValue");
        resultEndpoint.assertIsSatisfied();
    }

    @Override
    protected RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            public void configure() {
//                from("direct:start").filter(header("foo").isEqualTo("bar")).to("mock:result");
                from("direct:start").routeId("validationFile").validate().method("fileProcessor","valid").to("mock:result");
            }
        };
    }
}   

The test fails because fileProcessor is not found, yet I'm pretty sure my spring contextis properly loaded, I'm using the same beans.xmlfile for my dbunit tests and my DAO components are found just fine... What am I missing ?

EDIT: Thanks to Jérémis B's answer I fixed my problem easily. In case someone stumble as I did here is the code I added:

@Autowired
private FileProcessor fileProcessor;

@Override
protected JndiRegistry createRegistry() throws Exception {
    JndiRegistry registry = super.createRegistry();
    registry.bind("fileProcessor", fileProcessor);
    return registry;
}

Upvotes: 2

Views: 8948

Answers (1)

J&#233;r&#233;mie B
J&#233;r&#233;mie B

Reputation: 11022

You can see the official documentation for an "How to" test with Spring.

In your example, you create a Spring Context, but use the CamelTestSupport : This class create a CamelContext which is not aware of the Spring Context. The bean "fileProcessor" is not seen by this context.

There is a lot of ways to do this kind of test. The easiest, with the code you already have, is maybe to:

  • Inject the fileProcessor in your test class, with @Autowire
  • Override createRegistry and add the fileProcessor to the registry

You can too override CamelSpringTestSupport and implement createApplicationContext. Another way is to keep the route definition in a Spring Bean (through xml, or a RouteBuilder), and inject in your test MockEndpoints or ProducerTemplate.

Upvotes: 5

Related Questions