JUnit简单测试样例
源码
Main.java
package github.hyzeta;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class Main {
public static void main(String[] args) {
// write your code here
Result result = JUnitCore.runClasses(TriangleFunTest.class);
if (result.wasSuccessful()) {
System.out.println("All tests for triangle function has passed.");
} else {
System.out.println("Some tests for triangle function has failed.");
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
}
}
}
TriangleFun.java
package github.hyzeta;
enum TriangleFuncResult {
EQUILATERAL, ISOSCELES, SCALENE, ERROR
}
public class TriangleFun {
public TriangleFuncResult triangle(int a, int b, int c) {
if (a <= 0 || b <= 0 || c <= 0) {
return TriangleFuncResult.ERROR;
}
if (!(a + b > c && Math.abs(a - b) < c)) {
return TriangleFuncResult.ERROR;
}
if (a == b && b == c) {
return TriangleFuncResult.EQUILATERAL;
}
if (a == b || a == c || b == c) {
return TriangleFuncResult.ISOSCELES;
}
return TriangleFuncResult.SCALENE;
}
}
TriangleFunTest.java
package github.hyzeta;
import org.junit.Test;
import static org.junit.Assert.*;
public class TriangleFunTest {
@Test
public void testErrorTriangle() {
assertEquals(new TriangleFun().triangle(0, 0, 0), TriangleFuncResult.ERROR);
assertEquals(new TriangleFun().triangle(-1, -1, -1), TriangleFuncResult.ERROR);
assertEquals(new TriangleFun().triangle(-1, 2, 3), TriangleFuncResult.ERROR);
assertEquals(new TriangleFun().triangle(1, 2, 3), TriangleFuncResult.ERROR);
}
@Test
public void testEquilateralTriangle() {
assertEquals(new TriangleFun().triangle(3, 3, 3), TriangleFuncResult.EQUILATERAL);
assertEquals(new TriangleFun().triangle(1, 1, 1), TriangleFuncResult.EQUILATERAL);
assertEquals(new TriangleFun().triangle(2, 2, 2), TriangleFuncResult.EQUILATERAL);
}
@Test
public void testIsosceles() {
assertEquals(new TriangleFun().triangle(2, 2, 3), TriangleFuncResult.ISOSCELES);
assertEquals(new TriangleFun().triangle(4, 4, 5), TriangleFuncResult.ISOSCELES);
assertEquals(new TriangleFun().triangle(5, 7, 7), TriangleFuncResult.ISOSCELES);
}
@Test
public void testScalene() {
assertEquals(new TriangleFun().triangle(2, 3, 4), TriangleFuncResult.SCALENE);
assertEquals(new TriangleFun().triangle(3, 4, 5), TriangleFuncResult.SCALENE);
assertEquals(new TriangleFun().triangle(12, 22, 26), TriangleFuncResult.SCALENE);
}
}
JUnit和eclemma安装过程
直接在Maven仓库里搜索JUnit和hamcrest-core
http://search.maven.org/#search|gav|1|g:%22junit%22%20AND%20a:%22junit%22
http://search.maven.org/#search|gav|1|g:%22org.hamcrest%22%20AND%20a:%22hamcrest-core%22
下载junit-4.12.jar包和hamcrest-core-1.3.jar包
然后在IntelliJ IDEA里的Project Structure里的Libraries里添加这两个jar包
JUnit配置完成
eclemma的安装过程相同,直接将eclemma-2.3.3.zip加入到Libraries里就可以了,配置完成后如下
测试样例编写
详情请参考开头的源码,主要是编写了TriangleFun类(含有triangle函数)和TriangleFuncResult枚举类型,TriangleFuncResult枚举类型包含四个结果:
- 不是三角形:TriangleFuncResult.ERROR
- 等边:TriangleFuncResult.EQUILATERAL
- 等腰:TriangleFuncResult.ISOSCELES
- 非等腰三角形:TriangleFuncResult.SCALENE
然后编写测试类型TriangleFunTest,包含四个测试函数testErrorTriangle,testEquilateralTriangle,testIsosceles,testScalene,分别测试四种结果。
最后在Main函数中主动唤起JUnit测试
Result result = JUnitCore.runClasses(TriangleFunTest.class);
然后判断是否正确,如果测试通过则汇报成功,不成功则打印所有错误信息。
if (result.wasSuccessful()) {
System.out.println("All tests for triangle function has passed.");
} else {
System.out.println("Some tests for triangle function has failed.");
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
}
JUnit测试结果
代码覆盖率测试
之所以Main函数有部分没有覆盖到,是因为在Main中有部分代码用于判断和输出测试中的错误信息,而这个测试样例并没有发生错误,所以这段代码是跑不到的。