新建项目工程,新建被测类:Calculator

public class Calculator {
    private static int result; // 静态变量,用于存储运行结果
    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;
    }
    public String sayHello(){
        return "hello";
    }
}

与src平齐新建测试文件夹

idea junit简单实践

Ctrl+Shift+T 生成测试类

idea junit简单实践

idea junit简单实践

编辑测试类,运行测试类

idea junit简单实践

junit详细介绍:http://www.cnblogs.com/caoyuanzhanlang/p/3534846.html

转载于:https://my.oschina.net/ioo/blog/775539

相关文章: