先推荐两个不错的博客链接,如何使用junit
http://blog.csdn.net/andycpp/article/details/1327147
如果碰到一些困难,那么就看这个解决方法
http://blog.csdn.net/andycpp/article/details/1327147
测试三条边是否是可以组成三角形,三角形类型
public class Main { private static int result; // 静态变量,用于存储运行结果 public static int ERRTRIANGLE = 0; public static int ValidTRIANGLE = 1; public static int Equilateral = 1; public static int Isosceles = 2; public static int Scalene = 3; public void clear() { // 将结果清零 result = 0; } public int getResult() { return result; } public void validTrangle(int a,int b,int c){ if(b + c - a > 0 && b > 0 && a > 0 && c > 0)result = ValidTRIANGLE; else if(b + a - c > 0 && b > 0 && a > 0 && c > 0)result = ValidTRIANGLE; else if(c + a - b > 0 && b > 0 && a > 0 && c > 0)result = ValidTRIANGLE; else result = ERRTRIANGLE; } public void triangleType(int a,int b,int c){ if(a == b && b == c)result = Equilateral; else if(a == b || b == c || a == c)result = Isosceles; else result = Scalene; } }
import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; import static org.junit.Assert.*; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; public class MainTest { private static Main calculator = new Main(); @Before public void setUp() throws Exception { calculator.clear(); } @Test public void testTriangleType() { calculator.clear(); calculator.triangleType(10, 10, 10); assertTrue(calculator.getResult() == calculator.Equilateral); calculator.triangleType(10, 11, 10); assertTrue(calculator.getResult() == calculator.Isosceles); calculator.triangleType(10, 11, 12); assertTrue(calculator.getResult() == calculator.Scalene); } @Test public void testValidTrangle() { calculator.clear(); calculator.validTrangle(1, 11, 0); assertTrue(calculator.getResult() == calculator.ERRTRIANGLE); calculator.validTrangle(10, 11, 12); assertTrue(calculator.getResult() == calculator.ValidTRIANGLE); } }