Reputation: 2621
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?
Upvotes: 4
Views: 5862
Reputation: 2621
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;
Upvotes: 5