一、安装junit、hamcrest、eclemma
笔者用的是ideal,file --> project structure

左边侧栏选择libraries,左第二侧栏选择“+”(java),依次把需要的junit,hamcrest的jar包和eclemma解压后的文件包添加

二、编写判断三角形形状的代码
src下再建一个package,里面写判断三角形形状的class,目录结构如下:

判断的代码:
public class Triangle {
public static String Shape(int a, int b,int c) {
String shape = "";
if ((a + b <= c) || (a + c <= b) || (b + c <= a) || (a <= 0) || (b <= 0) || (c <= 0)) {
shape = "非三角形";
}
else if ((a == b) && (b == c)) {
shape = "等边三角形";
}
else if (((a == b) && (b != c)) || ((b == c) && (a != b)) || ((a == c) && (c != a))) {
shape = "等腰三角形";
}
else {
shape = "任意三角形";
}
return shape;
}
}
三、生成测试用例
新建一个test的文件夹存放测试用例:file --> new --> directory
public class Triangle {
}
选中要生成测试的类Triangle,使用快捷键option+return,选择create test ,在test文件夹下会自动生成测试用例文件

四、设计测试用例并执行
public class TriangleTest {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testShape1() throws Exception {
assertEquals((String)"非三角形",Triangle.Shape(2,2,4));
}
@Test
public void testShape2() throws Exception {
assertEquals((String)"非三角形",Triangle.Shape(2,2,5));
}
@Test
public void testShape3() throws Exception {
assertEquals((String)"非三角形",Triangle.Shape(-2,2,5));
}
@Test
public void testShape4() throws Exception {
assertEquals((String)"非三角形",Triangle.Shape(0,2,5));
}
@Test
public void testShape5() throws Exception {
assertEquals((String)"等边三角形",Triangle.Shape(2,2,2));
}
@Test
public void testShape6() throws Exception {
assertEquals((String)"等腰三角形",Triangle.Shape(2,2,3));
}
@Test
public void testShape7() throws Exception {
assertEquals((String)"任意三角形",Triangle.Shape(2,3,4));
}
}
五、执行结果

