Apache Camel Test Framework(MOCK)

http://jnn.iteye.com/blog/1327693

先说点题外话 :上周五我和几个朋友交流的时候我发现大家很少写测试,分析原因一个可能是大家认为程序还不复杂,我写测试的回报不高,还有一个原因可能是写单元测试很麻烦。 其实测试代码也可以写得很漂亮,而且一旦你针对自己的业务讲测试封装好了,其实写测试是很惬意的一件事。

 

对于我来说写Apache Camel的测试框架已经达到了这样的效果,在对Camel 代码除虫和添加新的功能的时候,我都很乐意写测试,因为有测试框架,我只需要花几分钟的时候就可以写完一个单元测试,我得到的回报是每天的工作都会测试帮我保驾护航,很容易重现用户报的bug,在准备产品发布的时候可以不用加班。

 

广告基本做完了,现在简单介绍一下和Camel业务相关的内容。了解Apache Camel的朋友应该知道,Camel作为一个实现了企业应用集成模式(EIP) 的消息媒介,其对客户展现的业务核心就是消息路由规则。由于Camel支持通过Java,Spring,Scala等 DSL来定义路由规则,一个具体的Camel应用其实是有不同的路由规则组成的,Camel 测试框架对其加载DSL部分的内容进行封装。Camel应用测试需要了解消息在Camel内部路由的具体情况以确保消息是以期望的方式进行路由的, 在Camel测试框架中MockEndpoint就充当了这样的角色,通过MockEndpoint你可以很方便地获取路由至此的消息,并对消息内容进行验证。

 

如果要使用Camel测试框架,你只需要在maven pom 中添加camel-test模块的依赖,针对你要测试的应用类型继承CamelTestSupport或者CamelSpringTestSupport, 相关的TestSupport会帮你搞定CamelContext,PrdoucerTemplate,ConsumerTemplate创建,以及相关路由规则加载的工作。这样你只需要在你的测试代码中针对你的路由准备好消息和使用MockEndpoint来验证消息路由的情况就可以了。

 

让我们来看一个具体的例子

Java代码 复制代码  收藏代码
  1. package org.apache.camel.test.patterns;   
  2.   
  3. import org.apache.camel.EndpointInject;   
  4. import org.apache.camel.Produce;   
  5. import org.apache.camel.ProducerTemplate;   
  6. import org.apache.camel.builder.RouteBuilder;   
  7. import org.apache.camel.component.mock.MockEndpoint;   
  8. import org.apache.camel.test.junit4.CamelTestSupport;   
  9. import org.junit.Test;   
  10.   
  11. public class FilterJUnit4Test extends CamelTestSupport {   
  12.   
  13.     @EndpointInject(uri = "mock:result")   
  14.     protected MockEndpoint resultEndpoint;   
  15.   
  16.     @Produce(uri = "direct:start")   
  17.     protected ProducerTemplate template;   
  18.   
  19.     @Test  
  20.     public void testSendMatchingMessage() throws Exception {   
  21.         String expectedBody = "<matched/>";   
  22.   
  23.         resultEndpoint.expectedBodiesReceived(expectedBody);   
  24.   
  25.         template.sendBodyAndHeader(expectedBody, "foo""bar");   
  26.   
  27.         resultEndpoint.assertIsSatisfied();   
  28.     }   
  29.   
  30.     @Test  
  31.     public void testSendNotMatchingMessage() throws Exception {   
  32.         resultEndpoint.expectedMessageCount(0);   
  33.   
  34.         template.sendBodyAndHeader("<notMatched/>""foo""notMatchedHeaderValue");   
  35.   
  36.         resultEndpoint.assertIsSatisfied();   
  37.     }   
  38.   
  39.     @Override  
  40.     protected RouteBuilder createRouteBuilder() {   
  41.         return new RouteBuilder() {   
  42.             public void configure() {   
  43.                 from("direct:start").filter(header("foo").isEqualTo("bar")).to("mock:result");   
  44.             }   
  45.         };   
  46.     }   
  47. }  
package org.apache.camel.test.patterns;

import org.apache.camel.EndpointInject;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class FilterJUnit4Test extends CamelTestSupport {

    @EndpointInject(uri = "mock:result")
    protected MockEndpoint resultEndpoint;

    @Produce(uri = "direct:start")
    protected ProducerTemplate template;

    @Test
    public void testSendMatchingMessage() throws Exception {
        String expectedBody = "<matched/>";

        resultEndpoint.expectedBodiesReceived(expectedBody);

        template.sendBodyAndHeader(expectedBody, "foo", "bar");

        resultEndpoint.assertIsSatisfied();
    }

    @Test
    public void testSendNotMatchingMessage() throws Exception {
        resultEndpoint.expectedMessageCount(0);

        template.sendBodyAndHeader("<notMatched/>", "foo", "notMatchedHeaderValue");

        resultEndpoint.assertIsSatisfied();
    }

    @Override
    protected RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            public void configure() {
                from("direct:start").filter(header("foo").isEqualTo("bar")).to("mock:result");
            }
        };
    }
}

 

首先在createRouteBuilder() 中定义了相关路由规则。这个路由是通过判断消息头foo的内容是否为bar来决定是否让消息通过。 其中 消息路由的入口是"direct:start" 节点, Camel测试框架支持通过annoation的方式注入节点(Endpoint)或者发送模板(ProducerTemplate),这样在测试代码中可以直接引用这些节点或者模版。

 

这样的测试是不是很直观呢,对于设置路由规则的开发这来说,他只需要将路由规则和相关的MockEndpoint的验证条件设置好,就可以跑测试了。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值