0

Suppose I have the following camel route:

.from("direct:start")
.log("received ${body} message")
.to("mock:end");

How would you test that the message "received Camel rocks!" message is logged when you send a "Camel rocks!" message to the direct:start endpoint

Alessandro Da Rugna
  • 4,571
  • 20
  • 40
  • 64
Julian
  • 3,678
  • 7
  • 40
  • 72
  • This is a similar question's answer: http://stackoverflow.com/questions/1827677/how-to-do-a-junit-assert-on-a-message-in-a-logger – Steve Huston May 12 '17 at 16:25

3 Answers3

1

I would read the written file. Or add a custom appender to the logging system and assert that it received the message.

Or check Camel's internal unit tests.

But what exactly are you trying to achieve?
You are supposed to test your application and not the frameworks you are using.

Alessandro Da Rugna
  • 4,571
  • 20
  • 40
  • 64
  • Thanks for the internal unit tests suggestion. I would follow up and see if I get some ideas. As for the use case question there are some situations when the application sends some special log entries with business implications; e.g. when something goes wrong those log entries may wake up a support guy. Rather then delaying the testing of this functionality when the system reaches a test environment I would prefer to be able to test it with an unit test that runs all the time I push a change. Hope it makes sense. – Julian May 10 '17 at 07:56
  • Yeah how do you test Java's System.out.println is printing what I typed ;). Its not worth the effort. And you have a mock endpoint just after the log, where you can assert that 1 message arrived, and the content in the message is as expected. If you really wanna test the log, then setup the logger to write to a file, and then read that file and check its content. – Claus Ibsen May 10 '17 at 11:55
  • @Julian Your test seems more like an integration test than a unit test to me. – Alessandro Da Rugna May 10 '17 at 12:31
  • I am accepting your answer as it provided with two ways to achieve what I wanted. However I was more after something like presented in my answer. Giving it is so invasive I cannot chose that one as being the best and would really love to hear camel is improved to support this kind of testing out of the box. See my suggestion at the top of my answer. – Julian May 11 '17 at 23:40
  • I'm in the same situation as OP. I believe that OP simply wants to assert the call to the logger, not the actual logger's behaviour. In some cases, log calls can be conditional. In those cases, your unit tests should ensure that the appropriate logger is called given the right circumstances. – Dark-Wynd Mar 25 '22 at 16:03
0

I would not test the actual logging part, but why not save the data you are interested in a property or header, and then in your unit test assert that the value of that property or header must be such and such?

Souciance Eqdam Rashti
  • 3,143
  • 3
  • 15
  • 31
0

Here is a way to test this which I admit is a bit too invasive. It would have been much easier if AdviceWithBuilder would have been added something like replaceWith(ProcessDefinition replacement).

Here is the working example:

package com.my.org.some.pkg;

import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.AdviceWithRouteBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.testng.CamelTestSupport;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.springframework.test.util.ReflectionTestUtils;
import org.testng.annotations.Test;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class CamelLoggingTest extends CamelTestSupport {

    @Override
    protected RoutesBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("direct:start").id("abc")
                .log("received ${body} message")
                .to("mock:stop");
            }
        };
    }

    @Test
    public void shouldLogExpectedMessage() throws Exception {
        Logger logger = Mockito.mock(Logger.class);
        context.getRouteDefinition("abc").adviceWith(context, new AdviceWithRouteBuilder() {
            @Override
            public void configure() throws Exception {
                ReflectionTestUtils.setField(context.getRouteDefinition("abc")
                    .getOutputs().get(0), "logger", logger);
            }
        });

        when(logger.isInfoEnabled()).thenReturn(true);
        sendBody("direct:start", "Camel rocks!");
        verify(logger).info("received Camel rocks! message");
    }
}
Julian
  • 3,678
  • 7
  • 40
  • 72