Apache camel with spring DSL and Junit Coverage

582 views Asked by At

I am completely new to apache camel. I got some basic understanding about it.

Now I am going through some videos and documents to get some ideas for implementing junit test cases for apache camel spring DSL based spring boot application but it's not clear to me since there are many ways to implement or in very high level.

I am confused.. which one to follow and what is actually happening in those junits

Does anyone have example or link or videos which explains junit coverage for apache camel spring DSL based spring boot application?

I am particularly looking for junits. also If you know someone good tutorials about apache camel let me know.

1

There are 1 answers

0
stringy05 On

JUnit and Camel doesn't work the same as JUnit and "normal" code and as far as I am aware there's only fairly rudimentary ways to get coverage of a camel route from JUnit. Camel routes are a processing model that is essentially an in memory model of the various steps that need to run, so you can't use code coverage tools to track what parts get executed.

Consider this route (in a subclass of RouteBuilder ):

public void configure() throws Exception { 
  from("jms:queue:zzz_in_document_q")
    .routeId("from_jms_to_processor_to_jms")
    .transacted()
    .log(LoggingLevel.INFO, "step 1/3: ${body}")
    .bean(DocBean.class)
    .log(LoggingLevel.INFO, "step 2/a3 - now I've got this: ${body}")
    .process(new DocProcessor())
    .log(LoggingLevel.INFO, "step 3/3 - and finally I've got this: ${body}")
    .to("jms:queue:zzz_out_document_q");
}

and an associated test case, in a class that extends CamelBaseTestSupport:

    @Test
    public void testJmsAndDbNoInsert() throws Exception {

        long docCountBefore = count("select * from document");
        template.sendBody("jms:queue:zzz_in_document_q", new Long(100));

        Exchange exchange = consumer.receive("jms:queue:zzz_out_document_q", 5000);
        assertNotNull(exchange);
        Document d = exchange.getIn().getBody(Document.class);
        assertNotNull(d);
        long docCountAfter = count("select * from document");
        assertEquals(docCountAfter, docCountBefore);

    }

When the unit test runs the app context will run the configure method, so I've got 100% coverage of my route before I even put a message on the queue! Except I don't, because all it's done is created the execution model in the camel route system and the various components and processors are now all going to run in the right order.

Beans and Processors will get included in the coverage reports, but if you have complex logic in the routes it's not going to give you coverage on this.

There is this capability, delivered around 2017 - https://issues.apache.org/jira/browse/CAMEL-8657 - but I haven't used it and am not sure how it will go working with whatever coverage tooling you use.