You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by "Harness, Josh" <Jo...@JTV.com> on 2015/08/25 20:04:28 UTC

Problem Modeling Tests After camel-example-spring-boot

Hi -

I'm new to camel, so please forgive me if I’ve got something wrong. I’ve got a camel spring boot project (no web container). I modeled my tests after the camel-example-spring-boot project but couldn’t get my camel mock endpoints to work. They’d get created but wouldn’t see any messages. After a lot of digging, I figured out that my mock was getting registered after the camel context started up.

My question is pretty simple:

Do mocks have to be created and registered prior to starting the camel context?

If so, I think there could be an issue with the camel-example-spring-boot project. I say this because I enabled tracing in the example project and see this:

13:08:57.387 [main] INFO  o.s.b.c.e.AnnotationConfigEmbeddedWebApplicationContext - Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@24313fcc: startup date [Tue Aug 25 13:08:57 EDT 2015]; root of context hierarchy
13:08:59.482 [localhost-startStop-1] INFO  o.a.c.c.C.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
13:08:59.482 [localhost-startStop-1] INFO  o.s.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 2099 ms
13:09:00.253 [localhost-startStop-1] INFO  o.s.b.c.e.FilterRegistrationBean - Mapping filter: 'applicationContextIdFilter' to: [/*]
13:09:00.802 [main] INFO  o.s.w.s.m.m.a.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@24313fcc: startup date [Tue Aug 25 13:08:57 EDT 2015]; root of context hierarchy
13:09:01.488 [main] INFO  o.a.camel.spring.SpringCamelContext - Apache Camel 2.16-SNAPSHOT (CamelContext: camel-1) is starting
13:09:01.730 [main] DEBUG o.apache.camel.impl.DefaultComponent - Creating endpoint uri=[mock://test], path=[test]
13:09:01.733 [main] DEBUG o.a.camel.spring.SpringCamelContext - mock://test converted to endpoint: Endpoint[mock://test] by component: org.apache.camel.component.mock.MockComponent@2eae4349
13:09:01.736 [main] DEBUG o.a.c.m.DefaultManagementAgent - Registered MBean with ObjectName: org.apache.camel:context=camel-1,type=endpoints,name="mock://test"
13:09:01.737 [main] DEBUG o.a.c.p.interceptor.DefaultChannel - Initialize channel for target: 'To[mock:test]'
13:09:01.744 [main] DEBUG o.a.camel.spring.SpringCamelContext - Warming up route id: route1 having autoStartup=true
13:09:01.746 [main] DEBUG org.apache.camel.impl.RouteService - Starting child service on route: route1 -> Pipeline[[Channel[Transform(Simple: ref:myBean)], Channel[sendTo(Endpoint[log://out])], Channel[sendTo(Endpoint[mock://test])]]]
13:09:01.754 [main] DEBUG o.a.c.component.mock.MockEndpoint$1 - Starting producer: Producer[mock://test]
13:09:01.754 [main] DEBUG o.a.c.processor.DefaultErrorHandler - Redelivery enabled: false on error handler: DefaultErrorHandler[TraceInterceptor[To[mock:test]]]
13:09:01.754 [main] DEBUG org.apache.camel.impl.ProducerCache - Adding to producer cache with key: Endpoint[mock://test] for producer: Producer[mock://test<mock://test]%20for%20producer:%20Producer[mock://test>]
13:09:01.754 [main] DEBUG org.apache.camel.impl.RouteService - Starting child service on route: route1 -> Pipeline[[Channel[Transform(Simple: ref:myBean)], Channel[sendTo(Endpoint[log://out])], Channel[sendTo(Endpoint[mock://test])]]]
13:09:01.790 [main] DEBUG org.apache.camel.impl.RouteService - Starting child service on route: route1 -> sendTo(Endpoint[mock://test])
13:09:01.794 [main] DEBUG org.apache.camel.impl.RouteService - Starting child service on route: route1 -> Channel[sendTo(Endpoint[mock://test])]
13:09:01.804 [main] DEBUG o.a.camel.spring.SpringCamelContext - Route: route1 >>> EventDrivenConsumerRoute[Endpoint[timer://trigger] -<timer://trigger]%20->> Pipeline[[Channel[Transform(Simple: ref:myBean)], Channel[sendTo(Endpoint[log://out])], Channel[sendTo(Endpoint[mock://test])]]]]
13:09:01.808 [main] DEBUG o.a.camel.spring.SpringCamelContext - Starting consumer (order: 1000) on route: route1
13:09:01.822 [main] INFO  o.a.camel.spring.SpringCamelContext - Route: route1 started and consuming from: Endpoint[timer://trigger]
13:09:01.824 [main] INFO  o.a.camel.spring.SpringCamelContext - Total 1 routes, of which 1 is started.
13:09:01.825 [main] INFO  o.a.camel.spring.SpringCamelContext - Apache Camel 2.16-SNAPSHOT (CamelContext: camel-1) started in 0.337 seconds

I’m thinking (but due to my newness, don’t know for sure) that the tests are “getting lucky” so to speak. This is because we see the "camel context starting" log statement shortly before the mock creation. I think the mock endpoint is be registered just prior to the context starting - thus making the test work. If the camel context started up more quickly, though, I suspect this test wouldn’t work (as is my case). Shouldn’t the mock be created and registered prior to attempting to start the context?

FWIW, I got my tests working by using a spring Configuration bean to return an instance of a CamelContextConfiguration class that programmatically creates mocks in the beforeApplicationStart method. It’s nowhere near as slick as the spring boot example, though.

Any ideas?

Thanks!

Josh Harness