1、实验02 基本路径测试法和单元测试工具JUnit的使用 实验学时:2学时 实验类型:验证 实验要求:必修 一、实验目的 l 掌握基本路径测试方法; l 了解在Eclipse环境下JUnit单元测试工具的安装和使用方法; l 针对一个实际问题,在JUnit环境下设计并执行测试用例。 二、实验要求 l 开发环境:Eclipse v3.7及以上版本;JUnit v4.10及以上版本;文本编辑软件。 l 硬件要求:CPU PIV 以上,256M 内存,1G 硬盘空间。 l 系统要求:Windows98/Me/XP/NT/2000,IE 5 以上。 三、实验内容 1. 下载并
2、安装JDK+Eclipse,学习Eclipse安装与使用方法。 2. 下载并安装JUnit,学习JUnit安装与使用方法。 3. 通读自动售货机程序,并将其在Eclipse环境下运行。问题描述,程序流程图和程序源代码见附录。 4. 按照如下格式,采用基本路径测试法编写测试用例(要有程序流程控制图,基本路径)。 编 号 输入值 Type 输入值 money 状态 预期输出 实际情况 1 Beer 5C 各资源剩余 Input Information Type: Beer; Money: 5 Cents; Change: 0 Current State
3、 Beer: 5 Orange Juice: 6 5 Cents: 7 1 Dollar: 6 与预期相同 2 OrangeJuice 5C 各资源剩余 Input Information Type: OrangeJuice; Money: 5 Cents; Change: 0 Current State Beer: 6 Orange Juice: 5 5 Cents: 7 1 Dollar: 6 与预期相同 3 Beer 1D 没有啤酒 Failure Information Beer Shortage …
4、5. 编写并执行基于JUnit的测试用例。 6. 提交测试用例程序和该程序运行结果最终画面。 7. 总结本次试验,并撰写实验报告。 四、实验结果检查与评定 l 提交时间:2012年3月5日之前/2012年3月12日之前 l 提交地址:学习委员邮箱 l 文档命名方式:09软件工程X班_0907052XXX_张三_实验01.doc 9 实验报告 实验序号: 实验项目名称: 学 号 姓 名 专业、班 实验地点 指导教师 实验时间 一、实验目的 二、实验要求 三、实验内容与步骤 四、分析与讨论
5、 五、教师评语 签名: 日期: 成绩 附录 问题描述 自动售货机程序: l 若投入5角钱或1元钱的硬币,按下“橙汁”或“啤酒”按钮,则相应的饮料就送出来; l 若售货机没有零钱找,则显示“零钱找完”的红灯亮,这是再投入一元硬币并按下按钮后,饮料不送出来而且1元硬币也退出来; l 若有零钱找,则显示“零钱找完”的红灯灭,在送出饮料的同时退还5角硬币。 程序流程图 程序源代码 public class SaleMachine { private int countOfBeer, countOfOrangeJuice, countOfFiveCent
6、s, countOfOneDollar; private String[] typeOfGoods = {"Beer", "OrangeJuice"}; private String resultOfDeal; public SaleMachine() { initial(); } public void initial() { countOfBeer = 6; countOfOrangeJuice = 6; countOfFiveCents = 6; countOfOneDollar = 6; } publi
7、c SaleMachine(int fiveCents, int oneDollar, int numOfBeer, int numOfOrange) //便于测试的初始化函数 { countOfFiveCents = fiveCents; countOfOneDollar = oneDollar; countOfBeer = numOfBeer; countOfOrangeJuice = numOfOrange; } public String currentState() { String state = "Current State
8、\n" + "Beer: " + countOfBeer + "\n" + "Orange Juice: " + countOfOrangeJuice + "\n" + "5 Cents: " + countOfFiveCents + "\n" + "1 Dollar: " + countOfOneDollar; return state; } public String operation(String type, String money) //type是用户选择的产品,money是用户投币种类
9、{ if(money.equalsIgnoreCase("5C")) //如果用户投入5角钱 { if(type.equals(typeOfGoods[0])) //如果用户选择啤酒 { if(countOfBeer>0) //如果还有啤酒 { countOfBeer--; countOfFiveCents++; resultOfDeal = "Input Information \n" + "Type: Beer; Money: 5 Cents; Change: 0\n\n" + cu
10、rrentState(); return resultOfDeal; } else { resultOfDeal = "Failure Information \n" + "Beer Shortage"; return resultOfDeal; } } else if (type.equals(typeOfGoods[1])) //用户选择橙汁 { if(countOfOrangeJuice > 0) { countOfOrangeJuice--;
11、countOfFiveCents++; resultOfDeal = "Input Information \n" + "Type: OrangeJuice; Money: 5 Cents; Change: 0\n\n" + currentState(); return resultOfDeal; } else { resultOfDeal = "Failure Information \n" + "Type Error"; return resultOfDeal; } }
12、 else { resultOfDeal = "Failure Information \n" + "Type Error"; return resultOfDeal; } } else if(money.equalsIgnoreCase("1D")) //如果用户投入一元钱 { if(countOfFiveCents>0) //如果用户投入一元钱 { if(countOfFiveCents > 0) //如果用户有零钱 { if(type.equals(typeOfGoods[0])
13、countOfBeer>0)//如果用户选择啤酒而且还有啤酒 { countOfBeer--; countOfFiveCents--; countOfOneDollar++; resultOfDeal = "Input Information \n" + "ype: Beer; Money: 1 Dollar; Change: 5 Cents\n\n" + currentState(); return resultOfDeal; } else if (type
14、equals(typeOfGoods[1]))//如果用户选择橙汁而且还有橙汁 { countOfOrangeJuice --; countOfFiveCents --; countOfOneDollar ++; resultOfDeal = "Input Information: \n" + "Type: OrangeJuice; Money: 1 Dollar; Change: 5 Cents\n\n" + currentState(); return resultOfDeal;
15、 } else { if(type.equals(typeOfGoods[0])&&countOfOrangeJuice<=0) { resultOfDeal = "Failue Information \n" + "Beer Shortage"; return resultOfDeal; } else if(type.equals(typeOfGoods[1])&&countOfOrangeJuice<=0) { resultOfDeal =
16、 "Failure Information \n" + "OrangeJuice Shortage"; return resultOfDeal; } else { resultOfDeal = "Failure Information \n" + "Type Error"; return resultOfDeal; } } } else { resultOfDeal = "Failure Information \n" + "Change
17、 Shortage"; return resultOfDeal; } } else { resultOfDeal = "Failure Information \n" + "Money Error"; return resultOfDeal; } } resultOfDeal = "Failure Information \n" + "Money Error"; return resultOfDeal; } } 测试用例实例代码: import static org.junit.Assert.
18、assertEquals; import junit.framework.*; public class TestSaleMachine extends TestCase{ public void testOperation1() //售货机各资源均有剩余,用户投币5角,选择啤酒 { SaleMachine saleMachine1 = new SaleMachine(); String expectedResult = "Input Information \n" + "Type: Beer; Money: 5 Cents; Chan
19、ge: 0\n\n" + "Current State\n" + "Beer: 5\n" + "Orange Juice: 6\n" + "5 Cents: 7\n" + "1 Dollar: 6"; assertEquals(expectedResult, saleMachine1.operation("Beer", "5C")); } public void testOperation2() //售货机各资源均有剩余,用户投币5角,选择橙汁 { SaleMachine s
20、aleMachine2 = new SaleMachine(); String expectedResult = "Input Information \n" + "Type: OrangeJuice; Money: 5 Cents; Change: 0\n\n" + "Current State\n" + "Beer: 6\n" + "Orange Juice: 5\n" + "5 Cents: 7\n" + "1 Dollar: 6"; assertEquals(expectedResult, saleMachine2
21、operation("OrangeJuice", "5C")); } public void testOperation3() //售货机没有啤酒,用户投币1元,选择啤酒 { SaleMachine saleMachine3 = new SaleMachine(6,6,0,6); String expectedResult = "Failure Information \n" + "Beer Shortage"; assertEquals(expectedResult, saleMachine3.operation("Beer", "1D")); } }






