很快进行进行手工单元测试,先看测试用例
/**
*
*/
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)
呵呵,越简单越好,很容易上手,到后面就越来越复杂,下一步生成测试报告