jeudi 1 octobre 2020

How to get @UseAdviceWith working in Camel 3

I am about writing some unit tests for production Camel routes in a Spring Boot application. To do so I use adviceWith technique and do not want the CamelContext to start before the advising is done. With Camel 2.24.2 my test class looks like a snippet below, I have ommited the details like autowiring and test expectations for brevity.

@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
@UseAdviceWith
public class CamelApplicationTests {
    ...
    context.getRouteDefinition("test.simple").adviceWith(context, new AdviceWithRouteBuilder() {
        @Override
        public void configure() throws Exception {
            interceptSendToEndpoint("file:/home/outfolder")
                .skipSendToOriginalEndpoint()
                .to("mock:outFolder");
            }
        });
    context.start();
    ...
}

Running this test I get in logs as expected

INFO  melSpringBootExecutionListener - @RunWith(CamelSpringBootRunner.class) before: class org.sample.CamelApplicationTests.testSimple
INFO  melSpringBootExecutionListener - Initialized CamelSpringBootRunner now ready to start CamelContext
INFO  spring.CamelAnnotationsHandler - Skipping starting CamelContext(s) as UseAdviceWith annotation was found and isUseAdviceWith is set to true.

Now, it's time for a migration towards Camel 3 and the advising part have to be rewritten as follows and it works just fine

@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
@UseAdviceWith
public class CamelApplicationTests {
    ...
    AdviceWithRouteBuilder.adviceWith(context, "test.simple", route ->
        route.interceptSendToEndpoint("file:/home/outfolder")
            .skipSendToOriginalEndpoint()
            .to("mock:outFolder"));
    context.start();
    ...
}

Except that CamelContext is getting start automatically, so we have the advised route restarted

INFO  o.a.c.i.e.InternalRouteStartupManager    : Route: test.simple started and consuming from: file:///home/infolder
INFO  o.a.c.impl.engine.AbstractCamelContext   : Total 1 routes, of which 1 are started
INFO  o.a.c.impl.engine.AbstractCamelContext   : Apache Camel 3.4.3 (camel) started in 0.923 seconds
INFO  org.sample.CamelApplicationTests         : Started  in 11.544 seconds (JVM running for 13.935)
INFO  o.a.c.i.engine.DefaultShutdownStrategy   : Starting to graceful shutdown 1 routes (timeout 45 seconds)
...
INFO  o.a.c.impl.engine.AbstractCamelContext   : Route: test.simple is stopped, was consuming from: file:///home/infolder
INFO  o.a.c.impl.engine.AbstractCamelContext   : Route: test.simple is shutdown and removed, was consuming from file:///home/infolder
INFO  org.apache.camel.reifier.RouteReifier    : AdviceWith route after: ...
...
INFO  o.a.c.i.e.InternalRouteStartupManager    : Route: test.simple started and consuming from: file:///home/infolder

It seems to be a bug, or am I missing something?

P.S.
After some researching I found an issue have been fixed in Camel 2.21 version, so I have checked the CamelSpringBootExecutionListener.java file from 3.4.x branch and it looks OK, but after taking a closer look at my logfile I noticed there was no @RunWith(CamelSpringBootRunner.class) preparing log, which means the prepareTestInstance method was never invoked, but I guess it should

Aucun commentaire:

Enregistrer un commentaire