You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Anton <ku...@gmail.com> on 2016/05/31 18:07:33 UTC

Came CDI tests start non-test routes

Hello

I have several unit tests that are annotated
with @RunWith(org.apache.camel.test.cdi.CamelCdiRunner.class) and also
inject

@Inject
@ContextName("camel-test-cdi")
CamelContext context;

I do not inject any non-test routes, yet, when I run the tests the routes
start.

How is it possible to run the tests without the non test routes from
starting?

Thanks

Re: Came CDI tests start non-test routes

Posted by Antonin Stefanutti <an...@stefanutti.fr>.
Hi Anton,

The Camel context beans managed by Camel CDI are automatically instantiated and started when the container is bootstrapped. Hence all the routes bound to them are started by default as well. And that’s the behaviour you get with the Camel CDI test runner as well.

If your need is to have your application routes (non-test routes) not started by default, one alternative is to customise the Camel context by setting the autoStartup property to false, e.g.:


@ApplicationScoped
public class ManualStartupCamelContext extends DefaultCamelContext {

    @PostConstruct
    void postConstruct() {
        setAutoStartup(false);
    }
}

If your need is to be able to perform some testing logic before the routes have started in your test class, to add some advices for example, you could declare an observer method in your test class for the CamelContextStartingEvent, e.g.:

void advice(@Observes CamelContextStartingEvent event, ModelCamelContext context) throws Exception {
        context.getRouteDefinition("route")
            .adviceWith(context, new AdviceWithRouteBuilder() {
                @Override
                public void configure() {
                    weaveAddLast().to("mock:messages");
                }
            });
    }

So that depends on what exactly is your need for not having your application routes started.

Let me know if that does not meet your need.

Antonin

> On 31 May 2016, at 20:07, Anton <ku...@gmail.com> wrote:
> 
> Hello
> 
> I have several unit tests that are annotated
> with @RunWith(org.apache.camel.test.cdi.CamelCdiRunner.class) and also
> inject
> 
> @Inject
> @ContextName("camel-test-cdi")
> CamelContext context;
> 
> I do not inject any non-test routes, yet, when I run the tests the routes
> start.
> 
> How is it possible to run the tests without the non test routes from
> starting?
> 
> Thanks