一、junit和hamcrest安装及引入
1.输入网址http://junit.org/junit4/下载junit及hamcrest
2.打开Eclipse新建一个project
3.右键点击build path 引进junit
二、eclemma安装
点击help-->Eclipse Marketplace-->在find中输入eclemma-->install-->重启Eclipse
三、三角形问题及junit、hamcrest、eclemma的使用
1、编写三角形判断程序
public class triangle_problem {
String triangle_juidje(double a,double b, double c){
if(a == b && a == c){
String str = new String();
str = "the trangle is equilateral";
return str;
}
if((a+b)>c &&(a+c)>b &&(b+c)>a){
if((a == b && a != c)|| (a==c && a != b) || (b == c && b != a)){
String str = new String();
str = "the trangle is isosceles";
return str;
}
if(a!=b && b!=c){
String str = new String();
str = "the trangle is scalene";
return str;
}
}
else{
String str = new String();
str = "the three edges can't make up a trangle!";
return str;
}
return null;
}
}
2、junit类生成
选中类-->new-->jUnit Test Case
3、编辑junit内容
import static org.junit.Assert.*;
import org.junit.Test;
public class triangle_problemTest {
private triangle_problem tri;
@Test
public void testequilateral() {
tri = new triangle_problem();
assertEquals("the trangle is equilateral",tri.triangle_juidje(3,3,3));
}
@Test
public void testisosceles() {
tri = new triangle_problem();
assertEquals("the trangle is isosceles",tri.triangle_juidje(2,3,2));
}
@Test
public void testscalene() {
tri = new triangle_problem();
assertEquals("the trangle is scalene",tri.triangle_juidje(2,3,4));
}
@Test
public void testnottrangle() {
tri = new triangle_problem();
assertEquals("the three edges can't make up a trangle!",tri.triangle_juidje(2,3,5));
}
}
4、运行junit文件
5、运行eclemma