小试:利用junit、eclemma测试代码
1.安装junit
将下载的jar加载进新建的项目即可
右键项目->properties->java build path->libraries->add external jars 选择jar的位置即可
2.安装eclemma
下载eclemma->help->install new software->add->选择下载好的eclemma压缩包的位置->next 等待安装(需要重启eclipse)
3.代码
三角形检测函数代码
package softwareTest; public class TriangleDetect { public TriangleDetect() { } public boolean isTri(int a1,int a2,int a3) { if(((a1+a2)>a3)&&((a1+a3)>a2)&&((a2+a3)>a1)) { return true; } else { return false; } } public String test(int b1,int b2,int b3) { if(isTri(b1,b2,b3)) { if((b1==b2)&&(b2==b3)) { return "equilateral"; } else if((b1==b2)||(b2==b3)||(b1==b3)) { return "isosceles"; } else { return "scalene"; } } else { return "not a triangle"; } } }
测试代码
package softwareTest; import java.util.Arrays; import java.util.Collection; import org.junit.*; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import org.junit.runner.RunWith; import static org.junit.Assert.*; @RunWith(Parameterized.class) public class TestPara { private int input1; private int input2; private int input3; private String expected; private TriangleDetect t; public TestPara(int input1,int input2,int input3,String expected){ this.input1 = input1; this.input2 = input2; this.input3 = input3; this.expected = expected; } @Before public void setUp(){ t = new TriangleDetect(); } @Parameters public static Collection<Object[]> getData(){ return Arrays.asList(new Object[][]{ {1,1,1,"equilateral"}, {2,3,3,"isosceles"}, {3,5,7,"scalene"} }); } @Test public void testTri() { assertEquals(this.expected,t.test(input1, input2, input3)); } }
4.运行结果:
5.eclemma测试结果