《2022年实验:单元测试_共5页 .pdf》由会员分享,可在线阅读,更多相关《2022年实验:单元测试_共5页 .pdf(5页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、本文档下载后根据实际情况可编辑修改使用实验题目:单元测试实验目的 :1、掌握 Junit4 的使用方法。2、能够使用Junit4 进行简单的测试工作。3、熟悉 EasyMock 的使用。实验设备 :主流 PC机一套, Windows 操作系统、 Eclipse、Junit、EclEmma和 EasyMock实验内容 :1、使用 Junit 对 ATMService 类中的方法进行单元测试。2、学习 EasyMock 的用法,写一个测试类TestATMServiceWithEMock 进行测试。3、撰写单元测试实验报告。附件 :/ATMService.javapackage atm;public
2、 class ATMService private IDataConnection dconn;public ATMService(IDataConnection dc)dconn=dc;/ 取款操作public void withdraw(String cardNo,int amount)AccountInfo a=dconn.getAccount(cardNo);int balance=a.getBalance()-amount;a.setBalance(balance);dconn.updateAccount(a);/ 存款操作public void deposit(String car
3、dNo,int amount)AccountInfo a=dconn.getAccount(cardNo);int balance=a.getBalance()+amount;a.setBalance(balance);dconn.updateAccount(a);/ 转账操作public void transfer(String fromCardNo,String toCardNo,int amount)AccountInfo a=dconn.getAccount(fromCardNo);AccountInfo b=dconn.getAccount(toCardNo);int aBalanc
4、e=a.getBalance()-amount;int bBalance=b.getBalance()+amount;精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 1 页,共 5 页本文档下载后根据实际情况可编辑修改使用a.setBalance(aBalance);b.setBalance(bBalance);dconn.updateAccount(a);dconn.updateAccount(b);/ 查询操作public int inquiry(String cardNo)AccountInfo a=dconn.getAccount(card
5、No);return a.getBalance();/IDataConnection.javapackage atm;public interface IDataConnection public AccountInfo getAccount(String id);public void updateAccount(AccountInfo account);public void addAccount(AccountInfo account);/AccountInfo.javapackage atm;public class AccountInfo private String cardNo;
6、private String password;private int balance;public AccountInfo(String c, String p, int b) cardNo = c;password = p;balance = b;精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 2 页,共 5 页本文档下载后根据实际情况可编辑修改使用public String getCardNo() return cardNo;public void setCardNo(String cardNo) this.cardNo = cardNo;p
7、ublic String getPassword() return password;public void setPassword(String password) this.password = password;public int getBalance() return balance;public void setBalance(int balance) this.balance = balance;/MockDataConnection.javapackage atm;import java.util.HashMap;public class MockDataConnection
8、implements IDataConnection private HashMap mdb;public MockDataConnection() mdb = new HashMap();AccountInfo a = new AccountInfo(1, 111111, 300);AccountInfo b = new AccountInfo(2, 222222, 1000);mdb.put(a.getCardNo(), a);mdb.put(b.getCardNo(), b);精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 3 页,共 5 页
9、本文档下载后根据实际情况可编辑修改使用/ Overridepublic AccountInfo getAccount(String id) / TODO Auto-generated method stubreturn mdb.get(id);/ Overridepublic void updateAccount(AccountInfo account) / TODO Auto-generated method stubaddAccount(account);/ Overridepublic void addAccount(AccountInfo account) / TODO Auto-ge
10、nerated method stubmdb.put(account.getCardNo(), account);/TestATMService.javapackage atm;import static org.junit.Assert.*;import org.junit.Test;public class TestATMService private IDataConnection mdc = new MockDataConnection();private ATMService atm = new ATMService(mdc);Testpublic void testWithdraw
11、() atm.withdraw(1, 20);AccountInfo a = mdc.getAccount(1);assertEquals(280, a.getBalance();精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 4 页,共 5 页本文档下载后根据实际情况可编辑修改使用Testpublic void testDeposit() atm.deposit(1, 20);AccountInfo a = mdc.getAccount(1);assertEquals(320, a.getBalance();Testpublic void testTransfer() atm.transfer(2, 1, 200);AccountInfo a = mdc.getAccount(1);AccountInfo b = mdc.getAccount(2);assertEquals(500, a.getBalance();assertEquals(800, b.getBalance();Testpublic void testInquiry() assertEquals(1000, atm.inquiry(2);精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 5 页,共 5 页
限制150内