2
votes

Edit: This is resolved see below

Hi all fellow Camel Riders!

I am testing a camel route and trying to automatically wire in mock endpoints.

I attempting to use @EndpointInject but they are not initiated during the unit test. My mock endpoints are null in my @Test method.

The start of my test class:

@RunWith(CamelSpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = CamelSpringDelegatingTestContextLoader.class)
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@DisableJmx(true)
@MockEndpoints("activemq*")
public class MyTest {

MyTest.java has an xml application context file that includes the basic camel context and other stuff. Also it is loading a @Configuration bean spring class that injects other services, and wires accordingly.

I have the following fields that I would like to be injected and autowired

@Autowired
private CamelContext camelContext;

@EndpointInject(uri = "mock://activemq:queue:b", context="camelContext")
protected MockEndpoint eventUpdatesQueue;

@Produce(uri = "activemq://queue:a?concurrentConsumers=10", context="camelContext")
protected ProducerTemplate testProducer;

The camelContext is autowiring properly, and I have printed out the endpoints keys and they are mocked out properly. But eventUpdatesQueue and testProducer are null.

I have resorted to writing code to instantiate everything, which is working fine:

eventUpdatesQueue = camelContext.getEndpoint("mock://activemq:queue:a", MockEndpoint.class);
Endpoint testProducer = camelContext.getEndpoint("activemq:queue:b?concurrentConsumers=1");

eventUpdatesQueue.expectedMessageCount(1);

Producer producer = testProducer.createProducer();
Exchange exchange = new DefaultExchange(camelContext);

exchange.getIn().setBody(body());
producer.process(exchange);
eventUpdatesQueue.assertIsSatisfied();

Which is working fine, but is tons more code then if @EndpointInject would work as I am expecting it to.

How do I go about debugging this? How do I get the mock endpoints and producer to be instantiated properly when I test? I am assuming that @EndpointInject would set the proper endpoint values magically for me. Is that incorrect?

1
This is resolved ... see notes - chrislovecnm
context="camelContext" is used to refer to a specific CamelContext by its name, such as when you have 2+ or more. So if you only have 1 CamelContext then do not use it, or if you do, make sure the name matches. - Claus Ibsen
Thanks Claus! Btw the new rest stuff looks cool, excited to use it when it goes ga. - chrislovecnm
Please mark this question as answered. - Peter Keller
Done - added my own comment - chrislovecnm

1 Answers

3
votes

So simple, but yet so hard. My camelContext was not named.

Broken:

@EndpointInject(uri = "mock://activemq:queue:b", context="camelContext")
protected MockEndpoint eventUpdatesQueue;

Simply remove context="camelContext" and as Shelley wrote ... it's alive.

Fixed:

@EndpointInject(uri = "mock://activemq:queue:b")
protected MockEndpoint eventUpdatesQueue;