Sunday, 4 February 2007

Complex expectations in EasyMock made easy using JSP EL

Setting up expectations in a mock framework like EasyMock is usually straightforward when dealing with simple types like integers or strings. But from time to time I run into situations when things become more complicated. Consider the following example.

We have a class Customer that has properties like name and address, but also a bunch of other things. We now want to test a method that populates the phone number field from some phone number directory. This test code could look something like this.


public void testPopulatePhoneNumber() throws Exception {
Services services = createMock(Services.class);
Customer loadedCustomer = new Customer();
loadedCustomer.setUserId("alb");
loadedCustomer.setFirstName("Al");
loadedCustomer.setLastName("Bundy");
// Set other properties
Customer saveCustomer = new Customer();
saveCustomer.setUserId("alb");
saveCustomer.setFirstName("Al");
saveCustomer.setLastName("Bundy");
saveCustomer.setPhoneNumber("555-SHOE");
// Set other properties
expect(services.loadCustomer("alb")).
andReturn(loadedCustomer);
expect(services.lookupPhoneNumber("Al", "Bundy")).
andReturn("555-SHOE");
services.saveCustomer(saveCustomer);
replay(services);

CustomerManager customerManager = new CustomerManager();
customerManager.populatePhoneNumber("alb");
verify(services);
}


(For simplicity I have a single interface for all sorts of good stuff, which of course wouldn't happen in a real system. Don't start building your own enterprise CRM system based on this. :-) )

The problem here is how you verify that the correct customer object is being passed into saveCustomer(). In this example EasyMock would use the equals() method of the Customer class to compare the expected object with the actual object. This means that to make all sorts of test cases work you would have to make the equals() method compare every single field in the class. Maybe this is not what you want. Maybe you want two customer objects to be considered equal if they have the same user id. But in this way the test cases have sort of hi-jacked the equals() method. Also you have to set up a lot of properties in the test cases and you get big equals() and hashCode() methods.

So I came up with the idea that you could use JSP EL (expression language) to write expressions that will make an assertion that the object is as expected. Writing an argument matcher for EasyMock that does this turned out to be pretty easy. Using this the expectation for saveCustomer() would become like this:


services.saveCustomer(assertBean(
"${bean.phoneNumber == '555-SHOE'}",
Customer.class));


Also you don't need the saveCustomer object any longer and the only properties that you have to set in the test case are those that actually has something to do with the test.

A small problem with this approoch though is that refactorings of the objects that are referenced within the EL expression will break the tests.

I have put up a small web page with the source code if you want to try it out at http://www.nilin.se/java/beanassert/.

39 comments:

keeleyresnick said...

援交友留言,視訊聊天室,成人貼圖站,情色視訊,情色論壇,美女圖片,080視訊聊天室,正妹牆qk176,18sxe成人影城,080視訊聊天室,免費交友,情色a片,台灣成人網,情色a片,聊天交友,台灣情色,情人貼圖,上班族聊天室f1,成人網,正妹交友,成人視訊,彰化人聊天室,台灣情色網,免費聊天,美女交友,丁字褲美女寫真,情色,免費視訊聊天室,777成人區,哈啦聊天室,0401視訊美女,免費色情影片,成人視訊,免費視訊,正妹牆自拍, ut聊天室,免費視訊,免費視訊g,八國聯軍成人,聊天室ut,

方便 said...

這麼好的部落格,以後看不到怎麼辦啊!!!..................................................

直到遠遠 said...

人必須心懷希望,才會活的快樂,日子才過得充實,有意義,有朝氣,有信心。........................................

知道 said...

志不立,天下無可成之事。........................................

別跟我說 said...

KK777一夜激情聊天live show成人自拍貼圖自慰少婦自拍裸體圖片台灣色情成人網站情人視訊網情色留言板視訊美女免費視訊聊天室限制級極度震撼情色論壇色情特區自拍裸女貼圖潮吹性影片觀賞小穴情色片a圖片sex story性愛影片美女做愛成人色情網站性愛圖片成人情色貼圖全裸寫真集圖片走光圖女生陰毛自慰影片色情av1007成人色情聊天室女生自慰裸體照成人影音聊天台灣色情網站色情片打手槍情色天堂成人視訊聊天免費情色網站av網超性感辣妹激突成人論壇情色視訊聊天鹹濕成人網站av成人論壇免費美女視訊

念強念強 said...

色情網自拍影片色情文章比基尼成人動畫色瞇瞇影片網小弟貼影片bt成人成人 影片日本成人網站日本成人網站破解日本成人網址日本成人線上免費日本成人免費影片日本成人動畫日本曾根日本有碼 dvd 專賣店日本有碼進口dvd專賣店日本東洋影片視訊 辣妹g8成人下載av短片-免費a片亞亞 dvd 光碟嘿咻kiss168cu成人bt情色 網4u成人0401影音視訊交友愛情館本土自拍xd成人圖區新人淚成人色網kkg亞洲免費影片av影片欣賞性行為補給站999成人性站最愛78論壇最色情的網站最色情的遊戲最多人聊天室最大a片網

MichellSommerville0202 said...

上床圖片av圖片免費色情圖台灣性網陰蒂淫妹貼圖色情激突成人論壇成人色情網激情成人聊天室情色影音鹹濕女生自慰影片淫慾自慰色情a片成人色情圖片網情人視訊網成人話題成人色情台灣成人淫蕩a圖片情色留言板女生奶頭情色成人全裸美女圖片情色寫真裸體照大奶子台灣色情成人網站一夜正妹美女裸體寫真g罩杯美女sex520情趣娃娃視訊打槍視訊辣妹uthome淫亂走光照av情色網女生高潮成人自拍火辣眉眉太太陰毛美女視訊聊天全裸入鏡火辣av三點全露

TerryMontanez2289 said...

Nice Post~!!!. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

雅伯 said...

Nice job!.................................................................

jon0301astabron said...

It's great!!..........................................

黃k0822oryb_card said...

唯有用熱情、用智慧去觀察事物,這事物才會把他的秘密,洩漏給我們...............................................................

ValarieEdmon靜宜 said...

很棒的分享~~~來留個言囉~~~~ ........................................

韋于倫成 said...

人生的價值以及他的快樂,都在於他有能力看重自己的生存..............................

柏勳 said...

君子立恆志,小人恆立志。..................................................

淑慧 said...

Constant dripping wears away the stone. 滴水穿石!加油! ....................................................

SadeRa盈君iford0412 said...

Where did you purchase this product?.................................................................

穆靜如 said...

^^~~輕輕鬆鬆的逛部落格,多謝有您的分享哦~~~ .................................................................

文群文群 said...

所有的資產,在不被諒解時,都成了負債.................................................................

佩春 said...

知識可以傳授,智慧卻不行。每個人必須成為他自己。.................................................................

美恭美恭美恭 said...

幸福不是一切,人還有責任。............................................................

天花天花 said...

如果成為一支火柴,也要點亮一個短暫的宇宙;如果是一隻烏鴉,也要叫疼閉塞的耳膜。.................................................................

原秋原秋 said...

好棒的地方 我一定要常來~~~^^~..................................................................

宜FH定博林P'韓T彥 said...

來看你了~心在、愛在、牽掛在,幸福才會繁衍不息^^............................................................

原秋原秋 said...

來塊巧克力吧~~心情會好哦~~............................................................

志張sf夏康如皓志gfg志 said...

憤怒,是片刻的瘋狂。..................................................

胡茹雨胡茹雨 said...

如此動感的blog!!!..................................................................

芸茂芸茂 said...

河水永遠是相同的,可是每一剎那又都是新的。. . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

盈廖生家秀蔡 said...

值得一看再看的格子,多謝分享.................................................................

佳瑩佳瑩 said...

世間事沒有一樣沒有困難,只要有信心去做,至少可以做出一些成績。..................................................

周旭威 said...

死亡是悲哀的,但活得不快樂更悲哀。. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

洪志源 said...

愛,拆開來是心和受兩個字。用心去接受對方的一切,用心去愛對方的所有。......................................................................

智鄭鄭堯智鄭鄭堯 said...

Learning makes a good man better and ill man worse.............................................................

承王蓁 said...

知識可以傳授,智慧卻不行。每個人必須成為他自己。. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

文王廷 said...

愛情是盲目的,但婚姻恢復了它的視力。......................................................................

偉曹琬 said...

期待你每一篇文章............................................................

峻胡邦慧v帆 said...

當最困難的時候,也就是離成功不遠的時候。..................................................

玉韓韓韓婷韓韓韓韓 said...

時間就是靈魂的生命。................. ................................................

怡靜怡靜怡靜怡雯 said...

每次看完你的文章,總是回味許久,要經常發表喔。..................................................

佳張張張張燕張張張張張 said...

Man proposes, God disposes..................................................................