UnitilsJUnit4 方法案例

   本着写单元测试奔覆盖率的精神。大概总结了已下几类写法,以方法为例:

   一:直接访问数据库层

@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 );
    }


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值