作业8:单元测试练习
一、对三角形判断代码作了稍微修改:使可以判断直角,等腰,等边,不等边
修改部分:
1 public String getType(Triangle triangle) { 2 String strType = "Illegal"; 3 4 // 判断是否是三角形 5 if (isTriangle(triangle)) { 6 7 long[] theContainer=triangle.getBorders(); 8 9 // 判断是否是等边三角形 10 if (triangle.lborderA == triangle.lborderB&&triangle.lborderA==triangle.lborderC) { 11 strType = "Regular"; 12 } 13 14 // 等腰三角形 15 else if((theContainer[1]==theContainer[2]&&theContainer[1]!=theContainer[0])|| 16 (theContainer[0]==theContainer[2]&&theContainer[0]!=theContainer[1])|| 17 (theContainer[0]==theContainer[1]&&theContainer[0]!=theContainer[2])){ 18 strType = "Isoceles"; 19 } 20 //判断是否是直角三角形 21 else if((theContainer[0]*theContainer[0]+theContainer[1]*theContainer[1]==theContainer[2]*theContainer[2])|| 22 (theContainer[0]*theContainer[0]+theContainer[2]*theContainer[2]==theContainer[1]*theContainer[1])|| 23 (theContainer[2]*theContainer[2]+theContainer[1]*theContainer[1]==theContainer[0]*theContainer[0])) 24 { 25 strType="rightTriangle"; 26 } 27 // 判断是否是不等边三角形 28 else if ((triangle.lborderA != triangle.lborderB) 29 && (triangle.lborderB != triangle.lborderC) 30 && (triangle.lborderA != triangle.lborderC)) { 31 strType = "Scalene"; 32 } 33 } 34 35 return strType; 36 }
测试用例如下:
序号 | 测试输入,三边 | 测试预言 |
1 | 3,3,3 | 等边 |
2 | 4,2,3 | 不等边 |
3 | 3,3,2 | 等腰 |
4 | 6,8,10 | 直角 |
二、运行截图:
三、心得体会
学会了基本的单元测试方法,单元测试代码主要设计思路就是根据输入的三边利用getType()方法得出三角形类型,主要也是测试这个,@Test注解,assertTrue断言,主要用到这两个。
ps: github代码地址:https://github.com/blocksmz/task8