Skip to content
Advertisement

Apache Camel mock endpoint

I recently started to investigate Apache Camel and I have one issue. I start writing some test for my routes, and there are a lot of examples, where “to” part of route is written as

    <route id="person-add-route">
        <from uri="direct:start"/>
        <to uri="mock:result"/>
    </route>

So, I wrote a test, where I am exepcting to have mock:result as last endproint.

@Test
@DirtiesContext
public void testCamel() throws Exception {
    // Given
    Object body = "body";
    int messageCount = 1;
    MockEndpoint endpoint = getMockEndpoint("mock:result");

    // When
    template.sendBody("direct:start", body);

    // Then
    endpoint.expectedMessageCount(messageCount);
    endpoint.assertIsSatisfied();
}

Here is the questions: Is this important to write mock:result if I want to test my route??

Advertisement

Answer

You don’t need to include “mock:result” in production, there are multiple ways to test your route. One is to implement isMockEndpoints in your Camel test:

@Override
public String isMockEndpoints()
{
     return "*";
}

So if your route is like this:

<route id="person-add-route">
    <from uri="direct:start"/>
    <to uri="direct:result"/>
</route>

You can check the MockEndpoint like this:

MockEndpoint endpoint = getMockEndpoint("mock:direct:result");

You can also use AdviceWith to modify your route at test time, by doing something like this:

context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
    @Override
    public void configure() throws Exception 
    {
         weaveAddLast().to("mock:result");
    }
});

Also, as Claus mentioned in his comment, make sure you set your expectations before you send your message to the route.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement