Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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??

like image 406
Ivan Ursul Avatar asked Dec 19 '22 09:12

Ivan Ursul


1 Answers

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.

like image 83
mdnghtblue Avatar answered Dec 29 '22 00:12

mdnghtblue