本例是加減乘除計算
先看一個簡單的類Calculator.java:
1
package com.test.junit;
2
/** *//**
3
*
4
* 數(shù)學(xué)計算
5
*
6
*/
7
public class Calculator
{
8
public int add(int a,int b)
{
9
return a + b;
10
}
11
public int minus(int a,int b)
{
12
return a - b;
13
}
14
public int multiply(int a, int b )
{
15
return a * b;
16
}
17
public int divide(int a , int b )throws Exception
18
{
19
if(0 == b)
{
20
throw new Exception("除數(shù)不能為零");
21
}
22
return a / b;
23
}
24
/**//*
25
* 一下是不用IDE給我們提供的Junit,用Junit本身去測試的3個方法。
26
public static void main (String[]args){
27
junit.textui.TestRunner.run(CalculatorTest.class);
28
junit.swingui.TestRunner.run(CalculatorTest.class);
29
junit.awtui.TestRunner.run(CalculatorTest.class);
30
}
31
*/
32
}
下面我們看看傳統(tǒng)的JUnit的TestCase:
CalculatorTest.java:
1
package com.test.junit;
2
3
import junit.framework.Assert;
4
import junit.framework.TestCase;
5
/** *//**
6
* Keep the bar green to keep the code clean
7
*
8
*在Junit3.8中必須繼承TestCase類
9
*
10
*單元測試不是證明您是對的,而是證明您沒有錯
11
*/
12
public class CalculatorTest extends TestCase
{
13
/** *//**
14
* 在Junit3.8中,測試方法滿足如下原則
15
* 1)public
16
* 2)void
17
* 3)無方法參數(shù)
18
* 4)最重要的方法名稱必須以test開頭
19
*/
20
private Calculator cal;
21
22
//在執(zhí)行每個test之前,都執(zhí)行setUp;
23
public void setUp()
{
24
cal = new Calculator();
25
}
26
27
//在執(zhí)行每個test之后,都要執(zhí)行tearDown
28
public void tearDown()
{
29
}
30
31
public void testAdd()
32
{
33
// Calculator cal = new Calculator();
34
int result = cal.add(1, 2);
35
//斷言assert
36
Assert.assertEquals(3, result);
37
}
38
39
public void testMinus()
40
{
41
// Calculator cal = new Calculator();
42
int result = cal.minus(5, 2);
43
Assert.assertEquals(3, result);
44
}
45
46
public void testMultiply()
47
{
48
Calculator cal = new Calculator();
49
int result = cal.multiply(4, 2);
50
Assert.assertEquals(8,result);
51
}
52
53
public void testDivide()
54
{
55
// Calculator cal = new Calculator();
56
int result = 0;
57
try
{
58
result = cal.divide(10,5);
59
} catch (Exception e)
{
60
e.printStackTrace();
61
//我們期望result = cal.divide(10,5);正常執(zhí)行;如果進入到catch中說明失敗;
62
//所以我們加上fail。
63
Assert.fail();//如果這行沒有執(zhí)行。說明這部分正確。
64
}
65
Assert.assertEquals(2,result);
66
}
67
68
public void testDivide2()
69
{
70
Throwable tx = null;
71
try
72
{
73
// Calculator cal = new Calculator();
74
cal.divide(10,0);
75
//正常來講cal.divide(10,0);已經(jīng)拋出異常,之后的代碼不會被執(zhí)行。
76
//我們也期望是這樣的。所以說如果下面的Assert.fail();執(zhí)行了。
77
//我的的測試就失敗了。
78
Assert.fail();//當(dāng)執(zhí)行到這里測試失敗,后面的代碼將不被執(zhí)行。
79
} catch (Exception e)
{
80
tx = e;
81
}
82
Assert.assertNotNull(tx);//斷言tx不為空。也就是說肯定有異常。
83
Assert.assertEquals(Exception.class,tx.getClass());//斷言tx的類型為Exception類型
84
Assert.assertEquals("除數(shù)不能為零", tx.getMessage());
85
}
86
}
本例中只有一個測試類。不過在開發(fā)過程中隨著項目的壯大,測試類也會越來越多。我們總不能每次測試都去挨個的執(zhí)行每個test類吧。所以我們要寫個總體的test類:
代碼如下:
1
package com.test.junit;
2
3
import junit.framework.Test;
4
import junit.framework.TestCase;
5
import junit.framework.TestSuite;
6
7
public class TestAll extends TestCase
{
8
public static Test suite()
9
{
10
TestSuite suite = new TestSuite();
11
suite.addTestSuite(CalculatorTest.class);//加入你所想測試的測試類
12
13
return suite;
14
}
15
}