本着写单元测试奔覆盖率的精神。大概总结了已下几类写法,以方法为例:
一:直接访问数据库层
@Test
public void queryProductsForAgentTest() throws DaoException
{
String userid = "133";
int scope = 1;
exportBOQDAO.queryProductsForAgent( scope, userid );
}
public List<ProductFamilyTreeInfo> queryProductsForAgent(int scope,String userid )
throws DaoException
{
Map<String, Object> para = new HashMap<String, Object>( 6 );
para.put( "USERID", userid );
para.put( "SCOPE", scope );
return super.queryForList( "exportboq.queryProductsForAgent", para );
}
二:有返回值
@Test
public void loadProductModelsTest() throws DaoException
{
List<String> modelIds = new ArrayList<String>( 3 );
modelIds.add( "12" );
EasyMock.expect( productModelFactory.loadProductModels( modelIds ) )
.andReturn( null );
EasyMockUnitils.replay();
List<IProductModel> result = model.loadProductModels( modelIds );
Assert.assertNull( result );
}
public List<IProductModel> loadProductModels( List<String> modelIds )
throws DaoException
{
return productModelFactory.loadProductModels( modelIds );
}
三:没有返回值
public ServiceProductModelInfo getLatestScvProductModelInfo(
String ModelID, int scope ) throws DaoException
{
return productModelDAO.queryLatestScvProductModelInfo( ModelID, scope );
}
@Test
public final void insertServicePdtModelPartTest() throws ServiceException
{
List<CFGServiceSpart> tempSvcSpartList = new ArrayList<CFGServiceSpart>();
productModelDAO.insertServicePdtModelPart( tempSvcSpartList );
EasyMock.expectLastCall();
EasyMockUnitils.replay();
model.insertServicePdtModelPart( tempSvcSpartList );
}
四:条件分支
public IConfigUnit copyConfigUnit( ConfigUnitInfo cfgUnitInfo,
IModelContainer targetContainer )
{
if ( cfgUnitInfo.getUnitType() == IModelConst.UNIT_TYPE.PRODUCT )
{
return this.productModelFactory.copyProductModel(
(ProductModelInfo)cfgUnitInfo, targetContainer );
}
else
{
return this.promotionModelFactory.copyPromotionModel(
(PromotionModelInfo)cfgUnitInfo, targetContainer );
}
}
@Test
public void copyConfigUnitTest() throws DaoException
{
ConfigUnitInfo cfgUnitInfo = new PromotionModelInfo();
ConfigUnitInfo cfgUnitInfo1 = new ProductModelInfo();
IModelContainer targetContainer = new BOQ( null );
EasyMock.expect( promotionModelFactory.copyPromotionModel( null, null ) )
.andReturn( null );
EasyMock.expect( productModelFactory.copyProductModel( null, null ) )
.andReturn( null );
EasyMockUnitils.replay();
model.copyConfigUnit( cfgUnitInfo, targetContainer );
model.copyConfigUnit( cfgUnitInfo1, targetContainer );
}