一、安装Junit及Hamcrest
1. 在Eclipse中新创建一个工程。
2. 右键->Build Path->Add External Libraries,添加Junit及Hamcrest。jar包导入完成
二、安装eclemma
1. 点击Help菜单---Install New Software,在弹出的对话框中,点击Add。
2. 输入Name,例如EclEmma。输入Location:http://update.eclemma.org/。
3. 点击Finish,Eclipse自动安装重启,安装完成。
三、判断是否为三角形
package lab; public class triangle { private static int result=0; public void TypeOfTriangle(int a,int b,int c) { if(a + b <= c || a + c <= b || b+ c <= a && a<=0 && b <= 0 && c <=0){ result = 1; //notTriangle if(a == b && a == c) result = 2;//Isosceles if(a == b || b == c || a == c) result = 3;//Equilateral else result = 4;//Scalene } } public int getResult(){ return result; } }
四、测试
package lab; import static org.junit.Assert.*; import org.junit.Test; public class triangleTest { private triangle triangle = new triangle(); @Test public void test() { triangle.TypeOfTriangle(0, 1, 2); assertEquals(1,triangle.getResult()); triangle.TypeOfTriangle(2, 2, 2); assertEquals(2,triangle.getResult()); triangle.TypeOfTriangle(2, 1, 2); assertEquals(3,triangle.getResult()); triangle.TypeOfTriangle(2, 3, 4); assertEquals(4,triangle.getResult()); } }
五、结果