Skip to content

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"/>

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

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

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

    // Then

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


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:

public String isMockEndpoints()
     return "*";

So if your route is like this:

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

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() {
    public void configure() throws Exception 

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