您好,欢迎访问三七文档
实验题目:单元测试实验目的:1、掌握Junit4的使用方法。2、能够使用Junit4进行简单的测试工作。3、熟悉EasyMock的使用。实验设备:主流PC机一套,Windows操作系统、Eclipse、Junit、EclEmma和EasyMock实验内容:1、使用Junit对ATMService类中的方法进行单元测试。2、学习EasyMock的用法,写一个测试类TestATMServiceWithEMock进行测试。3、撰写单元测试实验报告。附件://ATMService.javapackageatm;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();}}//IDataConnection.javapackageatm;publicinterfaceIDataConnection{publicAccountInfogetAccount(Stringid);publicvoidupdateAccount(AccountInfoaccount);publicvoidaddAccount(AccountInfoaccount);}//AccountInfo.javapackageatm;publicclassAccountInfo{privateStringcardNo;privateStringpassword;privateintbalance;publicAccountInfo(Stringc,Stringp,intb){cardNo=c;password=p;balance=b;}publicStringgetCardNo(){returncardNo;}publicvoidsetCardNo(StringcardNo){this.cardNo=cardNo;}publicStringgetPassword(){returnpassword;}publicvoidsetPassword(Stringpassword){this.password=password;}publicintgetBalance(){returnbalance;}publicvoidsetBalance(intbalance){this.balance=balance;}}//MockDataConnection.javapackageatm;importjava.util.HashMap;publicclassMockDataConnectionimplementsIDataConnection{privateHashMapString,AccountInfomdb;publicMockDataConnection(){mdb=newHashMapString,AccountInfo();AccountInfoa=newAccountInfo(1,111111,300);AccountInfob=newAccountInfo(2,222222,1000);mdb.put(a.getCardNo(),a);mdb.put(b.getCardNo(),b);}//@OverridepublicAccountInfogetAccount(Stringid){//TODOAuto-generatedmethodstubreturnmdb.get(id);}//@OverridepublicvoidupdateAccount(AccountInfoaccount){//TODOAuto-generatedmethodstubaddAccount(account);}//@OverridepublicvoidaddAccount(AccountInfoaccount){//TODOAuto-generatedmethodstubmdb.put(account.getCardNo(),account);}}//TestATMService.javapackageatm;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));}}
本文标题:实验:单元测试
链接地址:https://www.777doc.com/doc-5303127 .html