您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > 单元测试阶段--ATM取款机
软件测试方法和技术实验报告实验时间:2012年12月4日一、实验名称:单元测试阶段MockObject的用法(以ATM取款机为例)二、开发工具:Eclipse、JUnit、eclemma和EasyMock三、实验代码:(1)类ATMService:packageatm;publicclassATMService{privateIDataConnectiondconn;publicATMService(IDataConnectiondc){dconn=dc;}//取款操作publicvoidwithdraw(StringcardNo,intamount){AccountInfoa=dconn.getAccount(cardNo);intbalance=a.getBalance()-amount;a.setBalance(balance);dconn.updateAccount(a);}//存款操作publicvoiddeposit(StringcardNo,intamount){AccountInfoa=dconn.getAccount(cardNo);intbalance=a.getBalance()+amount;a.setBalance(balance);dconn.updateAccount(a);}//转账操作publicvoidtransfer(StringfromCardNo,StringtoCardNo,intamount){AccountInfoa=dconn.getAccount(fromCardNo);AccountInfob=dconn.getAccount(toCardNo);intaBalance=a.getBalance()-amount;intbBalance=b.getBalance()+amount;a.setBalance(aBalance);b.setBalance(bBalance);dconn.updateAccount(a);dconn.updateAccount(b);}//查询操作publicintinquiry(StringcardNo){AccountInfoa=dconn.getAccount(cardNo);returna.getBalance();}}(2)TestATMService单元测试用例packageatm;importstaticorg.junit.Assert.*;importorg.junit.Test;publicclassTestATMService{privateIDataConnectionmdc=newMockDataConnection();privateATMServiceatm=newATMService(mdc);@TestpublicvoidtestWithdraw(){atm.withdraw(1,20);AccountInfoa=mdc.getAccount(1);assertEquals(280,a.getBalance());}@TestpublicvoidtestDeposit(){atm.deposit(1,20);AccountInfoa=mdc.getAccount(1);assertEquals(320,a.getBalance());}@TestpublicvoidtestTransfer(){atm.transfer(2,1,200);AccountInfoa=mdc.getAccount(1);AccountInfob=mdc.getAccount(2);assertEquals(500,a.getBalance());assertEquals(800,b.getBalance());}@TestpublicvoidtestInquiry(){assertEquals(1000,atm.inquiry(2));}}测试用例执行情况:测试用例的覆盖情况:(3)TestATMServiceWithEMock单元测试用例packageatm;importstaticorg.easymock.EasyMock.*;importstaticorg.junit.Assert.*;importorg.junit.Before;importorg.junit.Test;publicclassTestATMServiceWithEMock{privateATMServiceatm;privateIDataConnectionmock;@BeforepublicvoidsetUp(){mock=createMock(IDataConnection.class);atm=newATMService(mock);}@TestpublicvoidtestTransfer(){AccountInfoa=newAccountInfo(1,111111,500);AccountInfob=newAccountInfo(2,222222,1000);//设置期望值expect(mock.getAccount(1)).andReturn(a);expect(mock.getAccount(2)).andReturn(b);mock.updateAccount(a);mock.updateAccount(b);replay(mock);//回放//执行被测方法atm.transfer(2,1,150);//验证执行被测方法后的状态assertEquals(650,a.getBalance());assertEquals(850,b.getBalance());verify(mock);}}测试用例执行情况:
本文标题:单元测试阶段--ATM取款机
链接地址:https://www.777doc.com/doc-3767369 .html