安裝過程:在bulid path上選擇junit,然後選擇Junit 4,其中自帶Junit和hamcrest,然後在eclipse Marketplace中安裝eclemma,最後重新開機eclipse。
Triangle.java:
package cn.tju.scs;
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
System.out.println("不合法!");
}
public boolean isEquilatera(){
if(one == two && one == three)
System.out.print("等邊! ");
else
System.out.print("非等邊! ");
return (one == two && one == three);
}
public boolean isIsosceles(){
if(one == two || one == three || two == three)
System.out.print("等腰! ");
else
System.out.print("非等腰! ");
return (one == two || one == three || two == three);
}
public boolean isScalene(){
if(one != two && one != three && two != three)
System.out.print("非等邊! ");
else
System.out.print("至少有一對邊相等! ");
return (one != two && one != three && two != three);
}
public static void main(String[] args){
Triangle tri = new Triangle(3,3,3);
System.out.println(tri.isEquilatera());
System.out.println(tri.isIsosceles());
System.out.println(tri.isScalene());
}
}

TestTriangle.java:
package cn.tju.scs;
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(3,3,3);
}
@Test
public void testIsEquilatera1() {
assertTrue(tri.isEquilatera());
}
@Test
public void testIsIsosceles() {
assertTrue(tri.isIsosceles());
}
@Test
public void testIsScalene() {
assertFalse(tri.isScalene());
}
}
轉載于:https://www.cnblogs.com/1828141578ts/p/8641231.html