一套測試就是一個強大的bug偵測器,能夠大大的縮短查找bug的時間。
本次我們主要了解使用Junit對代碼進行測試。
Junit中有以下這些方法可以對結果進行判定:
assertArrayEquals(expecteds, actuals) | 檢視兩個數組是否相等。 |
assertEquals(expected, actual) | 檢視兩個對象是否相等。類似于字元串比較使用的equals()方法 |
assertNotEquals(first, second) | 檢視兩個對象是否不相等。 |
assertNull(object) | 檢視對象是否為空。 |
assertNotNull(object) | 檢視對象是否不為空。 |
assertSame(expected, actual) | 檢視兩個對象的引用是否相等。類似于使用“==”比較兩個對象 |
assertNotSame(unexpected, actual) | 檢視兩個對象的引用是否不相等。類似于使用“!=”比較兩個對象 |
assertTrue(condition) | 檢視運作結果是否為true。 |
assertFalse(condition) | 檢視運作結果是否為false。 |
assertThat(actual, matcher) | 檢視實際值是否滿足指定的條件 |
fail() | 讓測試失敗 |
現在我們來測試檔案讀取器FileReader
第一步:準備一個測試檔案read,可以是任意格式的檔案,内容如下:
Lily 1 90 80 72
Jack 2 88 91 80
Peter 3 69 93 70
第二步:建立一個測試類TestFileReader
package File;
import static org.junit.Assert.*;
import java.io.FileReader;
import java.io.IOException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TestFileReader {
}
第三步:添加setUp和tearDown方法
setup用于産生檔案讀取對象,teardown用于删除檔案對象,因為Java中有自帶的垃圾回收處理機制,是以不需要額外的删除對象,在這裡我們使用teardown用來關閉檔案。

public class TestFileReader {
FileReader _input;
@Before
public void setUp() throws Exception {
try {
_input = new FileReader("E:/code/Rent/src/File/read");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("file can't open");
}
}
@After
public void tearDown() throws Exception {
try {
_input.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("file can't close");
}
}
}
setUp&&tearDown
第四步:添加測試代碼

@Test
public void testRead() throws IOException {
char ch='&';
for(int i = 0; i < 3; i++){
ch = (char) this._input.read();
}
assertEquals(ch,'l');
}
@Test
public void testReadAtEnd() throws IOException{
int ch = -111111;
for(int i = 0; i < 53; i++){
ch = this._input.read();
}
assertEquals(ch,-1);
}
Test Code
注意:我們這裡使用的是junit4,不是junit3.junit4比junit3更加友善簡潔。
這裡我們就微微的介紹一下junit4和junit3的不同。以及junit4的用法
不同:
1)junit4最大的特點就是引入了注釋,通過注釋來執行代碼。如:在setUp函數之上加before就是先執行,在tearDown上after就是後執行,在測試代碼上加test就是表示是測試代碼。不用再根據命名來執行。
2)junit4不需要繼承testcase類
3)內建測試時,直接在類之上注釋添加加入的單元測試
@RunWith(Suite.class)
@Suite.SuiteClasses({ TestCase1.class, TestCase2.class })
4)測試異常時,junit3是使用try-catch在異常之後添加fail。junit4添加@Test,使用參數“expected”,并指明抛出異常的Exception類:
//越界異常
@Test(expected = IndexOutOfBoundsException.class )
public void testCase2() {
/* test case 2*/
ArrayList emptyList = new ArrayList();
Object o = emptyList.get(0);
}
5)設定逾時,junit4使用"timeout"參數。如果測試運作的時間超過指定的毫秒數,則測試失敗。
@Test(timeout=3000)
public void remoteBaseRelativeResolutionWithDirectory()
throws IOException, ParsingException {
readBuilder.parse("config.xml" );
}
6)運作測試類,junit4不需要添加main函數,直接運作junit即可
7)參數化測試,junit4中的測試類不再使用構造函數添加參數,而是隻寫一個測試函數,把這若幹種情況作為參數傳遞進去,一次性的完成測試。在測試類的上面加@RunWith(Parameterized.class)
@RunWith(Parameterized.class)
public class JavaTest {
private int param;
private int param2;
private int result;
@Parameters public static Collection data() {
return Arrays.asList(new Object[][]{ { 2, 4, 6 }, { 0, 0, 0 }, { -3, 9, 6 } });
}
// 構造函數,對變量進行初始化
public JavaTest(int param, int param2, int result) {
this.param = param;
this.param2 = param2;
this.result = result;
}
@Test public void run() {
//do some thing use args, and assert it
int expected = param + param2;
assertEquals(expected, result);
}
}
http://www.cnblogs.com/Natural-way/p/5204407.html