标签:
一、junit、hamcrest和eclemma的安装。
1.junit和hamcrest在新建了junitHw1工程后在build path里通过add external jars添加junit-4.12.jar和hamcrest-all-1.3.jar。
2.eclemma在eclipse下点击help-eclipse marketplace-find-eclemma安装。
二、编写Triangle以及TestTriangle1。
1.Triangle-判断三角形类型
public class Triangle { int[] data; Triangle(){ data = new int[3]; } public void setData(int[] number){ for(int i = 0;i < data.length;i++){ data[i] = number[i]; } } public String checkTriangle(){ for(int i = 0;i < data.length;i++){ if(data[i]<=0) return "Illegal"; } if(data[0]+data[1]<=data[2]||data[0]+data[2]<=data[1]||data[1]+data[2]<=data[0]){ return "Not triangle"; } else if(data[0]==data[1]&&data[0]==data[2]){ return "Equilateral"; } else if(data[0]==data[1]||data[0]==data[2]||data[1]==data[2]){ return "Isosceles"; } else return "Scalene"; } }
2.TriangleTest
import static org.junit.Assert.*; import java.util.Arrays; import java.util.Collection; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class TriangleTest{ @Parameters public static Collection prepareData(){ return Arrays.asList(new Object[][]{{1,3,5,"Not triangle"},{1,2,-3,"Illegal"},{3,4,5,"Scalene"}, {2,2,2,"Equilateral"},{2,3,3,"Isosceles"}}); } public TriangleTest(int param1,int param2,int param3,String result){ this.param1 = param1; this.param2 = param2; this.param3 = param3; this.result = result; } @Test public void Test(){ triangle.setData(new int[]{param1,param2,param3}); assertEquals(result, triangle.checkTriangle()); } private int param1,param2,param3; private String result; private static Triangle triangle = new Triangle(); }
三、测试结果
标签:
原文地址:http://www.cnblogs.com/hillsblog/p/5296798.html