junit, hamcrest and eclemma.
a) junit的安装
步骤:
1. 从http://www.junit.org/ 下载junit相应的jar包;
2. 在CLASSPATH中加入JAR包所在的路径,如E:\Java\jar\junit\junit-4.10.jar;
3. 将junit-4.10.jar加入到项目的lib文件夹或者Libaries中;
4. Window -> Preference -> java -> JUinit(或者Window -> Show View -> Java -> JUnit),检测该项是否存在。若存在,则安装成功;
5. 建立测试用例:Right Click Package -> New -> Other -> Java -> JUnit -> JUnit Test Case;
6. 右键待测试方法, Run As -> JUnit Test。
b) hamcrest的安装
与junit安装一样
c) eclemma的安装
1) 先下载号eclemma,
2) 打开eclipse的help中install new software中的add 打开,在打开location,添加本地中的eclemma。
3) 使用时,右键项目,出现“Coverage As”选项,即说明Eclemma安装成功,选择一种方式运行即可。
b) 三角形问题的测试结果和覆盖报告
代码:
package triangle;
public class Triangle {
private int one;
private int two;
private int three;
public static boolean isLegal(int len1, int len2, int len3){
if(len1 <= 0 || len2 <= 0 || len3 <= 0)
return false;
if(len1 + len2 > len3 && len2 + len3 > len1 && len1 + len3 > len2)
return true;
return false;
}
public Triangle(int side_1, int side_2, int side_3){
if(isLegal(side_1, side_2, side_3)){
one = side_1;
two = side_2;
three = side_3;
}
else
one = two = three = 1;
}
public boolean isEquilatera(){
return (one == two && one == three);
}
public boolean isIsosceles(){
return (one == two || one == three || two == three);
}
public boolean isScalene(){
return !isEquilatera();
}
public static void main(String[] args){
Triangle tri = new Triangle(2,2,3);
System.out.println(tri.isEquilatera());
System.out.println(tri.isIsosceles());
System.out.println(tri.isScalene());
}
}
测试代码:
package triangle;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class TestTriangle {
private Triangle tri;
@Before
public void setUp() throws Exception {
tri=new Triangle(2,2,3);
}
@Test
public void testIsEquilatera() {
assertFalse(tri.isEquilatera());
}
@Test
public void testIsIsosceles() {
assertTrue(tri.isIsosceles());
}
@Test
public void testIsScalene() {
assertTrue(tri.isScalene());
}
}
测试结果:
覆盖测试: