panza
panza

Reputation: 1431

How to test a custom processor - Apache Camel Spring Test

I have the following route:

<camelContext xmlns="http://camel.apache.org/schema/spring">
        <route>
            <from uri="activemq:topic:inbox" />
            <log message="To: ${in.header.recipients}" />
            <to uri="bean:myLogger" />
        </route>
</camelContext>

bean:myLogger is my custom processor for formatting the log messages I am getting. The process method in my custom processor simply calls a private method that appends the type of message (e.g. CC) and the recipients' email. I am struggling to see how to test the actual resulting logs. I am using CamelSpringTestSupportand I am OK when it comes to testing the myLogger endpoint:

@Produce(uri = "activemq:topic:inbox")
    protected ProducerTemplate template;

    @Override
    protected AbstractApplicationContext createApplicationContext() {
        return new ClassPathXmlApplicationContext("file:src/main/resources/my-camel-context.xml");
    }

    @Test
        public void testLogEndpoint() throws Exception {
            String body = "Hello World";
            template.sendBody("activemq:topic:inbox", body);
            LOG.info("This is the message body we sent {} ", body);
        }

However, I am not really sure how to test the format of the returned logs. Do I send the email of the recipients in a similar fashion as the example above? But then how do I check whether the format is the correct one? I am really looking more for the approach than the actual solution.

Thank you so much for your help,

I.

Upvotes: 0

Views: 597

Answers (1)

Claus Ibsen
Claus Ibsen

Reputation: 55620

Make the log formatting in the bean independent on the logger such as another public/package method and then write a plain unit test that tests this method. Then you don't need to use Camel in that test at all.

If you can't do that, then maybe configure the logger to log to a file, and then read that file afterwards and test its logged as you want.

Upvotes: 1

Related Questions