今天用了一下spring-mock來測試系統中的dao.感覺真的不錯。這個很簡單,記下來得原因是怕自己會忘。
你的測試用例必須從AbstractDependencyInjectionSpringContextTests繼承。他會幫你創建beanfactory以及beans.但是你必須告訴他到那去找配置文件。這個工作就是通過getConfigLocations方法來完成。一般情況下,這個方法都很簡單。
看看我的就知道他要干些什么了。
@Override
????protected?String[]?getConfigLocations()?{
????????//?TODO?Auto-generated?method?stub
????????return?new?String[]{?"/springContext-hibernate.xml"?};
????}
好了,這樣就配置完成了。下面的工作就是獲取你要測試的對象,并對他測試了。
public?ShipMasterDao?getShipMasterDao()?{
????????if(shipMasterDao?==?null?){
????????????shipMasterDao?=?(ShipMasterDao)this.applicationContext.getBean("shipMasterDao");
????????}
????????return?shipMasterDao;
????}
????
????public?void?testGetUser(){
????????ShipMaster?shipMaster?=?this.getShipMasterDao().getShipMaster(1);
????????this.assertEquals(shipMaster.getImono(),?"imo01");
????}
嗯,很簡單吧。但是很有用。
記下,怕自己忘掉。