如何测试自定义处理器--Apache Camel Spring Test

时间:2016-11-08 17:26:56

标签: spring apache-camel activemq

我有以下路线:

<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是我的自定义处理器,用于格式化我收到的日志消息。我的自定义处理器中的process方法只是调用附加消息类型(例如CC)和收件人的私有方法。电子邮件。我很难看到如何测试实际生成的日志。我正在使用CamelSpringTestSupport,在测试myLogger端点时我很好:

@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);
        }

但是,我不确定如何测试返回日志的格式。我是否以与上述示例类似的方式发送收件人的电子邮件?但是,我如何检查格式是否正确?我真的在寻找比实际解决方案更多的方法。

非常感谢你的帮助,

予。

1 个答案:

答案 0 :(得分:1)

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.