标签:
一.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以及Triangletest:
1.判断三角形:
package junit; public class triangle { private static int equilateral = 1; private static int isosceles = 2; private static int scalene = 3; private static int error = 4; public int getEquilateral() { return equilateral; } public int getIsosceles() { return isosceles; } public int getScalene() { return scalene; } public int getError() { return error; } public int type(int a, int b, int c) { if(a<=0||b<=0||c<=0) { return error; } else if(a+b<c||a+c<b||b+c<a) { return error; } else if(a!=b&&b!=c&&a!=c) { return scalene; } else if(a==b&&b==c) { return equilateral; } else if(a==b||a==c||b==c) { return isosceles; } return 0; } }
2.测试:
package junit; import org.junit.Test; import static org.junit.Assert.*; public class triangletest { private triangle triangle = new triangle(); @Test public void testNottriangel(){ assertEquals(triangle.getError(),triangle.type(0, 2, 3)); } @Test public void testScalene(){ assertEquals(triangle.getScalene(),triangle.type(3, 2, 4)); } @Test public void testIsosceles(){ assertEquals(triangle.getIsosceles(),triangle.type(3, 2, 3)); } @Test public void testEquilateral(){ assertEquals(triangle.getEquilateral(),triangle.type(3, 3, 3)); } }
三.测试结果截图:
四个测试案例都通过,覆盖率94.9%
标签:
原文地址:http://www.cnblogs.com/hersenxiao/p/5293300.html