先说点题外话 :
上周五我和几个朋友交流的时候我发现大家很少写测试,分析原因一个可能是大家认为程序还不复杂,我写测试的回报不高,还有一个原因可能是写单元测试很麻烦。 其实测试代码也可以写得很漂亮,而且一旦你针对自己的业务讲测试封装好了,其实写测试是很惬意的一件事。
对于我来说写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来验证消息路由的情况就可以了。
让我们来看一个具体的例子
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的验证条件设置好,就可以跑测试了。
分享到:
相关推荐
ApacheCamel-JDBC Apache Camel JDBC组件 代码样例Demo
Apache Camel 开发使用指南中文版
ApacheCamel-FTP ApacheCamel-FTP Apache Camel FTP组件 Demo 样例
该文档来自阿帕奇2015中国路演。姜宁发表了题为“我和Apache Camel这些年/Years of Apache Camel and I”的主题演讲,欢迎下载!
01-ApacheCamel-HelloWorld Apache Camel Http helloworld Demo
05-ApacheCamel-CXF-WebService Apache Camel 集成 CXF组件,包含服务端、测试客户端
You will then see how components and endpoints handle exchanges in Camel, and how you can use them to create a complete and powerful mediation framework. You will finally learn how to tackle errors ...
camel-idea-plugin, Apache camel IntelliJ IDEA 插件 Apache camel IDEA插件 IntelliJ IDEA的插件,为代码编辑器提供一组 Apache camel 相关的功能。插件包括:Java,XML,属性或者yaml文件中驼峰终结点的代码完成...
简化软件集成:一个ApacheCamel教程在本教程中,您将了解集成大型软件的一些最大挑战,以及ApacheCamel如何轻松解决这些难题。在您的软件工程中,您可能至少做了一次以下操作:1.确定应启动数据发送的业务逻辑片段。...
骆驼开发指南,企业集成,路由表达式和判断,camel高级编程,api组件框架,耗时半年翻译成果,全部为markdown文档!!!
camel direct http jdbc mybatis 等等组件 骆驼 camel部分源码分析
apache camel for windows
Apache Camel 框架实现的JMS路由。有图解,很详细。
通过Spring Boot学习Apache Camel Framework通过Spring Boot学习Apache Camel Framework。 使用Active MQ,Kafka和REST API实施EI体系结构模式。 企业集成非常复杂。 微服务和云的发展使企业整合变得更加复杂。 您...
ApacheCamel-JMS-ActiveMQ Apache Camel JMS ActiveMQ 使用样例 1.从本地读取信息推送到MQ中 2.从MQ中读取信息保存到本地
此文档主要针对以下四个问题进行详细分析以及解答。 第一:我们要解决的企业应用集成问题。 第二:为什么使用Camel; 第三:Camel可以做什么? 第四:Camel的核心要素。
09-ApacheCamel-Timer Apache Camel Timer组件 定时器 代码Demo