生活随笔
收集整理的這篇文章主要介紹了
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代碼 ?
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() 中定義了相關路由規則。這個路由是通過判斷消息頭foo的內容是否為bar來決定是否讓消息通過。 其中 消息路由的入口是"direct:start" 節點, Camel測試框架支持通過annoation的方式注入節點(Endpoint)或者發送模板(ProducerTemplate),這樣在測試代碼中可以直接引用這些節點或者模版。
?
這樣的測試是不是很直觀呢,對于設置路由規則的開發這來說,他只需要將路由規則和相關的MockEndpoint的驗證條件設置好,就可以跑測試了。
?
總結
以上是生活随笔為你收集整理的Apache Camel Test Framework(MOCK)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。