今天用了一下spring-mock来测试系统中的dao.感觉真的不错。这个很简单,记下来得原因是怕自己会忘。
你的测试用例必须从AbstractDependencyInjectionSpringContextTests继承。他会帮你创建beanfactory以及beans.但是你必须告诉他到那去找配置文件。这个工作就是通过getConfigLocations方法来完成。一般情况下,这个方法都很简单。
看看我的就知道他要干些什么了。
@Override
protected String[] getConfigLocations() {
// TODO Auto-generated method stub
return new String[]{ " /springContext-hibernate.xml " };
}
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 " );
}
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 " );
}