JUnit和Eclemma使用的例子
测试的题目
Write a javaprogram for the triangle problem and test the program with Junit.
Description oftriangle problem:
Functiontriangle takes three integers a,b,cwhich are length of triangle sides; calculates whether the triangle isequilateral, isosceles, or scalene.
程序源代码
public class Triangle { /** * 给定三条边a、b、c * 判断是否可以构成一个三角形 * 若可以构成一个三角形,输出三角形的类型 **/ public int f_triangle(int i_a, int i_b, int i_c){ if(i_a+i_b>i_c&&i_b+i_c>i_a&&i_c+i_a>i_b){ if(i_a==i_b&&i_b==i_c){ System.out.println("这是一个等边三角形!"); return 1; }else if(i_a==i_b||i_b==i_c||i_c==i_a){ System.out.println("这是一个等腰三角形!"); return 2; }else{ System.out.println("这是一个三角形!"); return 3; } }else{ System.out.println("这不是一个三角形!"); return 0; } } }
JUnit编写的测试用例的代码
public class TriangleTest { Triangle c_t=new Triangle();; @Test public void testF_triangle_one() { assertEquals(0, c_t.f_triangle(1, 2, 3)); } @Test public void testF_triangle_one_01() { assertEquals(0, c_t.f_triangle(1, 3, 2)); } @Test public void testF_triangle_one_02() { assertEquals(0, c_t.f_triangle(2, 1, 3)); } @Test public void testF_triangle_one_03() { assertEquals(0, c_t.f_triangle(2, 3, 1)); } @Test public void testF_triangle_one_04() { assertEquals(0, c_t.f_triangle(3, 1, 2)); } @Test public void testF_triangle_one_05() { assertEquals(0, c_t.f_triangle(3, 2, 1)); } @Test public void testF_triangle_two() { assertEquals(1, c_t.f_triangle(3, 3, 3)); } @Test public void testF_triangle_three() { assertEquals(2, c_t.f_triangle(2, 3, 3)); } @Test public void testF_triangle_three_01() { assertEquals(2, c_t.f_triangle(2, 2, 3)); } @Test public void testF_triangle_three_02() { assertEquals(2, c_t.f_triangle(3, 2, 3)); } @Test public void testF_triangle_four() { assertEquals(3, c_t.f_triangle(3, 4, 5)); } @Test public void testF_triangle_four_01() { assertEquals(3, c_t.f_triangle(3, 5, 4)); } @Test public void testF_triangle_four_02() { assertEquals(3, c_t.f_triangle(4, 3, 5)); } @Test public void testF_triangle_four_03() { assertEquals(3, c_t.f_triangle(4, 5, 3)); } }
JUnit测试的结果
覆盖报告