实验报告:
(a)安装JUnit的简要描述,hamcrest和EclEmma。
下载好junit 和harmcrest 的jar 包,复制到工程的lib内,之后在上面点击右键,选定 Build it the path之后选择add , 在已经写好的类上就可以new一个新的junit test case , 之后在其中写测试代码。
Eclemma:打开eclipse->help->install new software->add->local->选择下载的eclemma,点OK就可以安装,然后重启,
(b)三角形问题的测试结果和覆盖报告(打印屏幕)。
判定三角形代码:
package text2; import org.junit.Before; public class text2 { public text2() { } @Before public int equ(int a,int b,int c) { boolean b1 = a + b > c; boolean b2 = a + c > b; boolean b3 = b + c > a; if(b1 && b2 && b3 && a > 0 && b > 0 && c > 0 ) { if(a == b && b == c ) { return 2; //等边 } else if( a == b || b==c || a==c) { return 1; //等腰 } else { return 0; //正常 } } else { return -1; //非三角形 } } }
测试代码:
package text2; import static org.junit.Assert.*; import org.junit.Test; public class test { private static text2 t = new text2(); @Test public void test() { //fail("Not yet implemented"); System.out.println(t.equ(4, 4, 4)); assertEquals(2,t.equ(4, 4, 4)); System.out.println("等边三角形"); assertEquals(-1,t.equ(0, 3, 4)); System.out.println("不成立三角形"); assertEquals(1,t.equ(4, 3, 3)); System.out.println("等腰三角形"); assertEquals(-1,t.equ(1, 3, 4)); System.out.println("不成立三角形"); assertEquals(0,t.equ(3, 4, 5)); System.out.println("斜三角形"); } }
测试结果:
覆盖率: