版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精选文档实验二Junit单元测试实验报告实验内容:利用Junit对实验程序Point2d.java进行单元测试实验目的:掌握单元测试的方法,掌握在Eclipse里进行Junit测试的技术。实验步骤和结果:1、 修改之前的Calculator的测试结果:(1)自动生成的CalculatorTest类 代码:package andycpp;public class Calculator private static int result; / 静态变量,用于存储运行结果 public void add(int n) result = result + n; public void substrac
2、t(int n) result = result - 1; /Bug: 正确的应该是 result =result-n public void multiply(int n) / 此方法尚未写好 public void divide(int n) result = result / n; public void square(int n) result = n * n; public void squareRoot(int n) for (; ;) ; /Bug : 死循环 public void clear() / 将结果清零 result = 0; public int getResult
3、() return result; (2)运行结果:自动生存的测试类完善测试类后的运行结果2、 修改和完善Calculator类:package andycpp;public class Calculator private static int result; / 静态变量,用于存储运行结果public void add(int n) result = result + n;public void substract(int n) result = result - n; /Bug: 正确的应该是 result =result-npublic void multiply(int n) res
4、ult =result*n; public void divide(int n) result = result / n;public void square(int n) result = n * n;public void squareRoot(int n) result=(int)Math.sqrt(n); /Bug : 死循环public void clear() / 将结果清零result = 0;public int getResult() return result;3、 修改后的Calculator的测试用例:(1) 加法:操作数:2,、3;结果:5(2) 减法:操作数:10、
5、2;结果:8(3) 乘法:操作数:10、2;结果:20(4) 除法:操作数:8、2;结果:4(5) 平方:操作数:3;结果:9(6) 开方:操作数:4;结果:2代码:package andycpp;import static org.junit.Assert.*;import org.junit.Before;import org.junit.Ignore;import org.junit.Test;public class CalculatorTest private static Calculator calculator = new Calculator();Beforepublic v
6、oid setUp() throws Exception calculator.clear();Testpublic void testAdd() calculator.add(2);calculator.add(3);assertEquals(5, calculator.getResult();Testpublic void testSubstract() calculator.add(10);calculator.substract(2);assertEquals(8, calculator.getResult();Testpublic void testMultiply() calcul
7、ator.add(10);calculator.multiply(2);assertEquals(20, calculator.getResult();Testpublic void testDivide() calculator.add(8);calculator.divide(2);assertEquals(4, calculator.getResult();Testpublic void testSquare() calculator.square(3); assertEquals(9, calculator.getResult();Test public void squareRoot
8、() calculator.squareRoot(4);assertEquals(2, calculator.getResult();4、 修改后的Calculator的测试结果:结果正确5、 Point2d的测试用例:测试用例1: pt1 = (0.0, 0.0)pt2 = (4.0, 3.0)import static org.junit.Assert.*;import org.junit.Before;import org.junit.Test;public class Point2dTest private static Point2d pt1=new Point2d();private static Point2d pt2=new Point2d(4.0,3.0);Beforepublic void setUp() throws Exception Testpublic void testDistanceFrom() assertEquals(5, pt1.dis
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GB/T 47272-2026煤焦油饱和分、芳香分、杂原子、酚类、胶质和沥青质含量的测定溶剂萃取-柱层析分离重量法
- 《玲珑剔透的美》教案-2025-2026学年赣美版小学美术四年级下册
- 2024年秋季新剑桥版英语三年级上册全册教案
- 中班语言动物谜语教案
- 海水倒灌应急物资管理
- 2025-2026学年黑龙江省高三第四次模拟考试化学试卷(含答案解析)
- 麻纺厂生产记录细则
- 某纺织厂原材料采购流程
- 麻纺产品销售渠道管理
- 2026年物理实验题结果分析答题模板
- 山东省潍坊市2026届高三下学期4月模拟考试(二模)政治试卷(含答案)
- 2026年4月河北保定市中考一模英语试卷
- 物流行业员工试用期考核标准
- WS∕T 391-2024 CT检查操作规程
- 疑难危重病例讨论模板
- MOOC 英文学术写作实战-北京大学 中国大学慕课答案
- T-BJCC 1003-2024 首店、首发活动、首发中心界定标准
- 小孩励志书籍读后感
- 全麻苏醒期躁动处理
- 2024年吉林长春天然气集团有限公司招聘笔试参考题库含答案解析
- 外科学:胃十二指肠外科疾病(英文版)完整版
评论
0/150
提交评论