Java unit 测试工具使用步骤
1.安装插件
(1)Clover:
下载zip包,解压,把jar包放入Eclipse的plugins文件夹下,重启Eclipse。Windows-preferences下找到Clover选项,加入license(cmd下用命令java CenquaKeyGen(在该压缩包下)生成一个license)。重启后,单击右键project,找到Clover绿色按键。
(2)EvoSuite:
help->Install New Software-add:输入EvoSuite Eclipse插件的地址:http://www.evosuite.org/update。安装该插件
2.创建一个java project。以Calculator为例子:
(1)包含project-pakage-class:名字Calculator
(2)class内容:
public class Calculator {
private static int result = 0; // 静态变量,用于存储运行结果
public void add(int n) {
result = result + n;
}
public void substract(int n) {
result = result - 1; //Bug: 正确的应该是 result =result-n
}
public void multiply(int n){
} // 此方法尚未写好
public void divide(int n) {
result = result / n;
}
public void square(int n) {
result = n * n;
}
public void squareRoot(int n) {
for (; ;) ; //Bug : 死循环
}
public void clear() { // 将结果清零
result = 0;
}
public int getResult() {
return result;
}
}
2.导入junit相关jar包
三个包:hamcrest-core-1.3.rc2.jar
hamcrest-library-1.3.RC2.jar
junit-4.11.jar
3.右键project Clover enable project
4.右键要测试的Class,选中generate test with EvoSuite
5.生成新的包和测试class,打开测试class,右键选择run as junit test
tags:java