关于Java手工编译项目应用之三 junit单元测试

  很快进行进行手工单元测试,先看测试用例

/**
 *
 */
package com.jsw.app;

/**
 * @author gang.huang
 *
 */
public class Calculation {

    public int add(int a, int b) {
        return a + b;
    }

    public int subtraction(int a, int b) {
        return a - b;
    }

    public int multiplication(int a, int b) {
        return a * b;
    }

    public double division(double a, double b) {
        if (b == 0) {
            return 0;
        }
        return a / b;

    }

}

单元测试代码为:

package com.jsw.app;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class CalculationTest {
   
    private Calculation calculation;
    @Before
    public void setUp() throws Exception {
        this.calculation= new Calculation();
    }

    @After
    public void tearDown() throws Exception {
        this.calculation=null;
    }

    @Test
    public void testAdd() {
        Assert.assertEquals(3, this.calculation.add(1, 2));
    }

    @Test
    public void testSubtraction() {
        Assert.assertEquals(6, this.calculation.subtraction(7, 1));
       
    }

    @Test
    public void testMultiplication() {
        Assert.assertEquals(6, this.calculation.multiplication(6, 1));
    }


}

 

现在用Java命令进行编译(在这里我全部用到绝对路径 ):

D:\project_test\hellotest\src>javac -classpath D:\project_test\hellotest\lib\junit-4.8.1.jar -d D:\project_test\hellotest\bin  D:\project_test\hellotest\src\com\jsw\app\*.java

 

运行我们测试代码:

D:\project_test\hellotest\bin>java -classpath %CLASSPATH%;D:\project_test\hellotest\lib\junit-4.8.1.jar org.junit.runner
.JUnitCore com.jsw.app.CalculationTest
JUnit version 4.8.1
...
Time: 0

OK (3 tests)

 

呵呵,越简单越好,很容易上手,到后面就越来越复杂,下一步生成测试报告

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值