Tasks:
- Install Junit(4.12), Hamcrest(1.3) with Eclipse
- Install Eclemma with Eclipse
- Write a java program for the triangle problem and test the program with Junit.
a) Description of triangle problem:
Function triangle takes three integers a,b,c which are length of triangle sides; calculates whether the triangle is equilateral, isosceles, or scalene.
1.安装junit 和 hamcrest
首先在官网下载hamcrest-all-1.3和junit-4.12
新建工程st_lab1 然后build path->configure build path->add library 和 add externel library 添加依赖项
2.安装eclemma
在hlep->market搜寻安装eclemma 之后选择重启
3.测试用例
(1)写三角形判定函数
package st_lab1; public class Triangle { private int a,b,c; public Triangle(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } public int getKind(int a, int b, int c) { int Kind = 3; if(a + b <= c || a+c <= b || b+c <= a) { Kind = -1; return Kind; } if( a == b && b == c) { Kind = 0; return Kind; } else if(a == c || b == c || a == b) { Kind = 1; return Kind; } else { Kind = 2; return Kind; } } }
(2)参数化用例
import static org.junit.Assert.*; import java.util.Arrays; import java.util.Collection; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import st_lab1.Triangle; @RunWith(Parameterized.class) public class TriangleTest { private int a; private int b; private int c; private int expected; private Triangle triangle; public TriangleTest(int expected,int a, int b,int c) { this.expected = expected; this.a = a; this.b = b; this.c = c; } @Before public void setUp() throws Exception { triangle = new Triangle(a, b, c); } @After public void tearDown() throws Exception { } @Parameters public static Collection<Object[]> getData() { return Arrays.asList(new Object[][] { {0,2,2,2}, {1,5,2,5}, {1,2,2,3}, {2,1,2,3}, {-1,7,6,1}, {2,2,3,4}, {-1,2,5,8}, }); } @Test public void testGetKind() { assertEquals(this.expected, triangle.getKind(a, b, c)); } }
4.测试结果