LB1_3014218162
实验名称
Junit and Eclemma
实验目标
Install Junit and write a Java program with it.
实验内容
1.Install Junit(4.12), Hamcrest(1.3) with Eclipse
2.Install Eclemma with Eclipse
3.Write a java program for the triangle problem and test the program with Junit.
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 and eclemma
2.Write a java program for the triangle problem and test the program with Junit.
code:
1 package lab1; 2 3 public class Triangle { 4 public static String triangleshape(int a,int b, int c){ 5 if(a == b && a == c && b == c){ 6 return "equilateral"; 7 } 8 else if(a == b || a == c || b == c){ 9 return "isosceles"; 10 } 11 else{ 12 return "scalene"; 13 } 14 } 15 }
1 package lab1; 2 3 import static org.junit.Assert.*; 4 import java.util.Arrays; 5 import java.util.Collection; 6 import org.junit.Test; 7 import org.junit.runner.RunWith; 8 import org.junit.runners.Parameterized; 9 import org.junit.runners.Parameterized.Parameters; 10 11 @RunWith(Parameterized.class) 12 public class Testing { 13 14 private int a; 15 private int b; 16 private int c; 17 private String expected; 18 private String result = null; 19 20 public Testing(int a,int b, int c, String expected){ 21 this.a = a; 22 this.b = b; 23 this.c = c; 24 this.expected= expected; 25 } 26 27 @Parameters 28 public static Collection<Object[]> getData(){ 29 return Arrays.asList(new Object[][]{ 30 {1,1,1,"equilateral"}, 31 {2,3,4,"scalene"}, 32 {3,5,5,"isosceles"}, 33 {6,6,8,"isosceles"} 34 }); 35 } 36 37 @Test 38 public void test() { 39 assertEquals(this.expected,Triangle.triangleshape(a,b,c)); 40 } 41
3、run