生活随笔
收集整理的這篇文章主要介紹了
Apache Camel Test Framework(MOCK)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
http://jnn.iteye.com/blog/1327693
先說(shuō)點(diǎn)題外話 :上周五我和幾個(gè)朋友交流的時(shí)候我發(fā)現(xiàn)大家很少寫(xiě)測(cè)試,分析原因一個(gè)可能是大家認(rèn)為程序還不復(fù)雜,我寫(xiě)測(cè)試的回報(bào)不高,還有一個(gè)原因可能是寫(xiě)單元測(cè)試很麻煩。 其實(shí)測(cè)試代碼也可以寫(xiě)得很漂亮,而且一旦你針對(duì)自己的業(yè)務(wù)講測(cè)試封裝好了,其實(shí)寫(xiě)測(cè)試是很愜意的一件事。
?
對(duì)于我來(lái)說(shuō)寫(xiě)Apache Camel的測(cè)試框架已經(jīng)達(dá)到了這樣的效果,在對(duì)Camel 代碼除蟲(chóng)和添加新的功能的時(shí)候,我都很樂(lè)意寫(xiě)測(cè)試,因?yàn)橛袦y(cè)試框架,我只需要花幾分鐘的時(shí)候就可以寫(xiě)完一個(gè)單元測(cè)試,我得到的回報(bào)是每天的工作都會(huì)測(cè)試幫我保駕護(hù)航,很容易重現(xiàn)用戶報(bào)的bug,在準(zhǔn)備產(chǎn)品發(fā)布的時(shí)候可以不用加班。
?
廣告基本做完了,現(xiàn)在簡(jiǎn)單介紹一下和Camel業(yè)務(wù)相關(guān)的內(nèi)容。了解Apache Camel的朋友應(yīng)該知道,Camel作為一個(gè)實(shí)現(xiàn)了企業(yè)應(yīng)用集成模式(EIP) 的消息媒介,其對(duì)客戶展現(xiàn)的業(yè)務(wù)核心就是消息路由規(guī)則。由于Camel支持通過(guò)Java,Spring,Scala等 DSL來(lái)定義路由規(guī)則,一個(gè)具體的Camel應(yīng)用其實(shí)是有不同的路由規(guī)則組成的,Camel 測(cè)試框架對(duì)其加載DSL部分的內(nèi)容進(jìn)行封裝。Camel應(yīng)用測(cè)試需要了解消息在Camel內(nèi)部路由的具體情況以確保消息是以期望的方式進(jìn)行路由的, 在Camel測(cè)試框架中MockEndpoint就充當(dāng)了這樣的角色,通過(guò)MockEndpoint你可以很方便地獲取路由至此的消息,并對(duì)消息內(nèi)容進(jìn)行驗(yàn)證。
?
如果要使用Camel測(cè)試框架,你只需要在maven pom 中添加camel-test模塊的依賴,針對(duì)你要測(cè)試的應(yīng)用類型繼承CamelTestSupport或者CamelSpringTestSupport, 相關(guān)的TestSupport會(huì)幫你搞定CamelContext,PrdoucerTemplate,ConsumerTemplate創(chuàng)建,以及相關(guān)路由規(guī)則加載的工作。這樣你只需要在你的測(cè)試代碼中針對(duì)你的路由準(zhǔn)備好消息和使用MockEndpoint來(lái)驗(yàn)證消息路由的情況就可以了。
?
讓我們來(lái)看一個(gè)具體的例子
Java代碼 ?
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"); ??????????????} ??????????}; ??????} ??}??
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;@Testpublic void testSendMatchingMessage() throws Exception {String expectedBody = "<matched/>";resultEndpoint.expectedBodiesReceived(expectedBody);template.sendBodyAndHeader(expectedBody, "foo", "bar");resultEndpoint.assertIsSatisfied();}@Testpublic void testSendNotMatchingMessage() throws Exception {resultEndpoint.expectedMessageCount(0);template.sendBodyAndHeader("<notMatched/>", "foo", "notMatchedHeaderValue");resultEndpoint.assertIsSatisfied();}@Overrideprotected RouteBuilder createRouteBuilder() {return new RouteBuilder() {public void configure() {from("direct:start").filter(header("foo").isEqualTo("bar")).to("mock:result");}};}
}
?
首先在createRouteBuilder() 中定義了相關(guān)路由規(guī)則。這個(gè)路由是通過(guò)判斷消息頭foo的內(nèi)容是否為bar來(lái)決定是否讓消息通過(guò)。 其中 消息路由的入口是"direct:start" 節(jié)點(diǎn), Camel測(cè)試框架支持通過(guò)annoation的方式注入節(jié)點(diǎn)(Endpoint)或者發(fā)送模板(ProducerTemplate),這樣在測(cè)試代碼中可以直接引用這些節(jié)點(diǎn)或者模版。
?
這樣的測(cè)試是不是很直觀呢,對(duì)于設(shè)置路由規(guī)則的開(kāi)發(fā)這來(lái)說(shuō),他只需要將路由規(guī)則和相關(guān)的MockEndpoint的驗(yàn)證條件設(shè)置好,就可以跑測(cè)試了。
?
總結(jié)
以上是生活随笔為你收集整理的Apache Camel Test Framework(MOCK)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。