一、junit和hamcrest安裝及引入
1.輸入網(wǎng)址http://junit.org/junit4/下載junit及hamcrest
2.打開Eclipse新建一個(gè)project
3.右鍵點(diǎn)擊build path 引進(jìn)junit
二、eclemma安裝
點(diǎn)擊help-->Eclipse Marketplace-->在find中輸入eclemma-->install-->重啟Eclipse
三、三角形問(wèn)題及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內(nèi)容
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、運(yùn)行junit文件
5、運(yùn)行eclemma