2

Camel route testing with Springboot, Activemq & mock endpoint

I've a Camel route which reads form activemq & passes to processor which does further processing & business logic.

I'm trying to test by producing & sending message by ProducerTemplate, creating mock endpoint "mock:result" & weaving as the last node for the route & doing assert on it. It's not satisfying the asserts.

Camel Route:

from("queue:myIncomingQueue?username=***&password=***")
    .doTry()
        .log(LoggingLevel.INFO, "Incoming Message: [ body:${body} ]")
        .to("bean-validator:validateIncomingMessage")
        .unmarshal(myUnmarshller.format())
        .setHeader(Constants.MESSAGE_VALID, constant(true))
    .endDoTry()
    .doCatch(Exception.class)
        .log(LoggingLevel.ERROR, 
            "failed to parse message [ body:${body} ], Exception - "
                + exceptionMessage())
    .end()
    .choice()
        .when(header(Constants.MESSAGE_VALID).isNotNull())
            .doTry()
                .process(myProcessor)
            .endDoTry()
            .doCatch(Exception.class)
                .log(LoggingLevel.ERROR, 
                    "failed to process message [ body:${body} ], Exception - "
                        + exceptionMessage())
            .end()
        .endChoice()
    .end();

Test Class:

@RunWith(CamelSpringBootRunner.class)
@SpringBootTest(classes = {Application.class}, properties = { 
    "camel.springboot.java-routes-include-pattern=**/MyRoute*"
})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@MockEndpoints("mock:result")
public class MyRouteTest {

    @Autowired
    private CamelContext camelContext;

    @Produce(uri="queue:myIncomingQueue?username=***&password=***")
    private ProducerTemplate template;

    @EndpointInject(uri = "mock:result")
    private MockEndpoint mockOutput;

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

    @Test
    public void messagesuccessful() throws Exception {
        Exchange dummyExchange = this.generateDataExchange();
        mockOutput.expectedMessageCount(1);
        mockOutput.expectedBodiesReceived(dummyExchange.getIn().getBody());
        mockOutput.message(0).header("API-X-HEADER").isEqualTo(123);

        template.send(dummyExchange);
        Thread.sleep(6000);
        mockOutput.assertIsSatisfied();
    }
}

Result:

> java.lang.AssertionError: mock://result Received message count. Expected: <1> 
but was: <0>
Expected :<1> 
Actual   :<0>
<Click to see difference>
Roman Vottner
  • 12,213
  • 5
  • 46
  • 63
  • Have you tried to add the `@UseAdviceWith` annotation to your test class? Also, instead of a `Thread.sleep(...)` you should look at [NotifyBuilder](http://camel.apache.org/notifybuilder.html). You can also simplify the test class by runnig it with `SpringRunner` and replace `Application.class` with your Route to test. If you do not define `@EnableAutoConfiguration` you should add the test config and `CamelAutoConfiguration` to the `@SpringBootTest` classes as well. A simple skeleton can be seen [here](https://stackoverflow.com/a/51892629/1377895). – Roman Vottner Aug 21 '18 at 12:20
  • In regards to the test error, have you tried to enable Camel's debug log and/or [tracing capabilities](http://camel.apache.org/tracer.html) and see where things go wrong? For simplicity you can also replace the from-endpoint with a `direct:start` by adding `replaceFromWith("direct:start")` in your route-weaving. – Roman Vottner Aug 21 '18 at 12:22

0 Answers0