新聞中心
JUnit 中的重要的 API
JUnit 中的最重要的程序包是 ?junit.framework? 它包含了所有的核心類。一些重要的類列示如下:

目前創(chuàng)新互聯(lián)已為1000多家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機(jī)、網(wǎng)站托管、服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、惠山網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
| 序號 | 類的名稱 | 類的功能 |
|---|---|---|
| 1 | Assert | ?assert ?方法的集合 |
| 2 | TestCase | 一個(gè)定義了運(yùn)行多重測試的固定裝置 |
| 3 | TestResult | ?TestResult ?集合了執(zhí)行測試樣例的所有結(jié)果 |
| 4 | TestSuite | ?TestSuite ?是測試的集合 |
Assert 類
下面介紹的是 ?org.junit.Assert? 類:
public class Assert extends java.lang.Object這個(gè)類提供了一系列的編寫測試的有用的聲明方法。只有失敗的聲明方法才會被記錄。?Assert ?類的重要方法列式如下:
| 序號 | 方法和描述 |
|---|---|
| 1 | ??void assertEquals?(boolean expected, boolean actual) ?檢查兩個(gè)變量或者等式是否平衡 |
| 2 | ??void assertFalse?(boolean condition) ?檢查條件是假的 |
| 3 | ???檢查對象不是空的 |
| 4 | ???檢查對象是空的 |
| 5 | ???檢查條件為真 |
| 6 | ???在沒有報(bào)告的情況下使測試不通過 |
下面讓我們在例子中來測試一下上面提到的一些方法。在 C:\ > JUNIT_WORKSPACE 目錄下創(chuàng)建一個(gè)名為 ?TestJunit1.java? 的類。
import org.junit.Test;
import static org.junit.Assert.*;
public class TestJunit1 {
@Test
public void testAdd() {
//test data
int num= 5;
String temp= null;
String str= "Junit is working fine";
//check for equality
assertEquals("Junit is working fine", str);
//check for false condition
assertFalse(num > 6);
//check for not null value
assertNotNull(str);
}
}接下來,我們在 C:\ > JUNIT_WORKSPACE 目錄下創(chuàng)建一個(gè)文件名為? TestRunner1.java? 的類來執(zhí)行測試案例。
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner1 {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestJunit1.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
} 用 javac 編譯? Test case ?和 ?Test Runner ?類
C:\JUNIT_WORKSPACE>javac TestJunit1.java TestRunner1.java現(xiàn)在運(yùn)行 ?Test Runner ?它將運(yùn)行在 ?Test Case? 類中定義并提供的測試樣例。
C:\JUNIT_WORKSPACE>java TestRunner1檢查輸出結(jié)果。
true
TestCase 類
下面介紹的是? org.junit.TestCaset? 類:
public abstract class TestCase extends Assert implements Test測試樣例定義了運(yùn)行多重測試的固定格式。?TestCase? 類的一些重要方法列式如下:
| 序號 | 方法和描述 |
|---|---|
| 1 | ?int countTestCases()? 為被run(TestResult result) 執(zhí)行的測試案例計(jì)數(shù) |
| 2 | ?TestResult createResult()? 創(chuàng)建一個(gè)默認(rèn)的 TestResult 對象 |
| 3 | ?String getName()? 獲取 TestCase 的名稱 |
| 4 | ?TestResult run()? 一個(gè)運(yùn)行這個(gè)測試的方便的方法,收集由TestResult 對象產(chǎn)生的結(jié)果 |
| 5 | ?void run(TestResult result)? 在 TestResult 中運(yùn)行測試案例并收集結(jié)果 |
| 6 | ?void setName(String name)? 設(shè)置 TestCase 的名稱 |
| 7 | ?void setUp()? 創(chuàng)建固定裝置,例如,打開一個(gè)網(wǎng)絡(luò)連接 |
| 8 | ?void tearDown()? 拆除固定裝置,例如,關(guān)閉一個(gè)網(wǎng)絡(luò)連接 |
| 9 | ?String toString()? 返回測試案例的一個(gè)字符串表示 |
我們在例子中嘗試一下上文提到的方法。在 C:\ > JUNIT_WORKSPACE 路徑下創(chuàng)建一個(gè)名為?TestJunit2.java ? 的類。
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
public class TestJunit2 extends TestCase {
protected double fValue1;
protected double fValue2;
@Before
public void setUp() {
fValue1= 2.0;
fValue2= 3.0;
}
@Test
public void testAdd() {
//count the number of test cases
System.out.println("No of Test Case = "+ this.countTestCases());
//test getName
String name= this.getName();
System.out.println("Test Case Name = "+ name);
//test setName
this.setName("testNewAdd");
String newName= this.getName();
System.out.println("Updated Test Case Name = "+ newName);
}
//tearDown used to close the connection or clean up activities
public void tearDown( ) {
}
}接下來,在 C:\ > JUNIT_WORKSPACE 路徑下創(chuàng)建一個(gè)名為 ?TestRunner2.java? 的類來執(zhí)行測試案例。
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner2 {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestJunit2.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}用 javac 編譯? Test case? 和 ?Test Runner? 類
C:\JUNIT_WORKSPACE>javac TestJunit2.java TestRunner2.java現(xiàn)在運(yùn)行? Test Runner? 它將運(yùn)行在 ?Test Case? 類中定義并提供的測試樣例。
C:\JUNIT_WORKSPACE>java TestRunner2檢查輸出結(jié)果。
No of Test Case = 1
Test Case Name = testAdd
Updated Test Case Name = testNewAdd
true
TestResult 類
下面定義的是? org.junit.TestResult ?類:
public class TestResult extends Object?TestResult ?類收集所有執(zhí)行測試案例的結(jié)果。它是收集參數(shù)層面的一個(gè)實(shí)例。這個(gè)實(shí)驗(yàn)框架區(qū)分失敗和錯(cuò)誤。失敗是可以預(yù)料的并且可以通過假設(shè)來檢查。錯(cuò)誤是不可預(yù)料的問題就像 ?ArrayIndexOutOfBoundsException?。?TestResult? 類的一些重要方法列式如下:
| 序號 | 方法和描述 |
|---|---|
| 1 | ??? 在錯(cuò)誤列表中加入一個(gè)錯(cuò)誤 |
| 2 | ??? 在失敗列表中加入一個(gè)失敗 |
| 3 | ??? 顯示測試被編譯的這個(gè)結(jié)果 |
| 4 | ?int errorCount()? 獲取被檢測出錯(cuò)誤的數(shù)量 |
| 5 | ?Enumeration errors()? 返回錯(cuò)誤的詳細(xì)信息 |
| 6 | ?int failureCount()? 獲取被檢測出的失敗的數(shù)量 |
| 7 | ?void run(TestCase test?) 運(yùn)行 TestCase |
| 8 | ?int runCount()? 獲得運(yùn)行測試的數(shù)量 |
| 9 | ?void startTest(Test test)? 聲明一個(gè)測試即將開始 |
| 10 | ?void stop()? 標(biāo)明測試必須停止 |
在 C:\ > JUNIT_WORKSPACE 路徑下創(chuàng)建一個(gè)名為? TestJunit3.java? 的類。
import org.junit.Test;
import junit.framework.AssertionFailedError;
import junit.framework.TestResult;
public class TestJunit3 extends TestResult {
// add the error
public synchronized void addError(Test test, Throwable t) {
super.addError((junit.framework.Test) test, t);
}
// add the failure
public synchronized void addFailure(Test test, AssertionFailedError t) {
super.addFailure((junit.framework.Test) test, t);
}
@Test
public void testAdd() {
// add any test
}
// Marks that the test run should stop.
public synchronized void stop() {
//stop the test here
}
}接下來,在 C:\ > JUNIT_WORKSPACE 路徑下創(chuàng)建一個(gè)名為 ?TestRunner3.java? 的類來執(zhí)行測試案例。
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner3 {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestJunit3.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
} 用 javac 編譯 ?Test case ?和? Test Runner? 類
C:\JUNIT_WORKSPACE>javac TestJunit3.java TestRunner3.java現(xiàn)在運(yùn)行? Test Runner ?它將運(yùn)行在 ?Test Case? 類中定義并提供的測試樣例。
C:\JUNIT_WORKSPACE>java TestRunner3檢查輸出結(jié)果。
true
TestSuite 類
下面定義的是? org.junit.TestSuite? 類:
public class TestSuite extends Object implements Test?TestSuite? 類是測試的組成部分。它運(yùn)行了很多的測試案例。?TestSuite? 類的一些重要方法列式如下:
| 序號 | 方法和描述 |
|---|---|
| 1 | ?void addTest(Test test) ? 在套中加入測試。 |
| 2 | ?void addTestSuite(Class extends TestCase> testClass)? 將已經(jīng)給定的類中的測試加到套中。 |
| 3 | ?int countTestCases()? 對這個(gè)測試即將運(yùn)行的測試案例進(jìn)行計(jì)數(shù)。 |
| 4 | ?String getName()? 返回套的名稱。 |
| 5 | ?void run(TestResult result)? 在 TestResult 中運(yùn)行測試并收集結(jié)果。 |
| 6 | ?void setName(String name)? 設(shè)置套的名稱。 |
| 7 | ?Test testAt(int index)? 在給定的目錄中返回測試。 |
| 8 | ?int testCount()? 返回套中測試的數(shù)量。 |
| 9 | ?static Test warning(String message)? 返回會失敗的測試并且記錄警告信息。 |
在 C:\ > JUNIT_WORKSPACE 路徑下創(chuàng)建一個(gè)名為 ?JunitTestSuite.java? 的類。
import junit.framework.*;
public class JunitTestSuite {
public static void main(String[] a) {
// add the test's in the suite
TestSuite suite = new TestSuite(TestJunit1.class, TestJunit2.class, TestJunit3.class );
TestResult result = new TestResult();
suite.run(result);
System.out.println("Number of test cases = " + result.runCount());
}
}用 javac 編譯 ?Test suit ?
C:\JUNIT_WORKSPACE>javac JunitTestSuite.java現(xiàn)在運(yùn)行 ?Test Suit ?
C:\JUNIT_WORKSPACE>java JunitTestSuite檢查輸出結(jié)果。
No of Test Case = 1
Test Case Name = testAdd
Updated Test Case Name = testNewAdd
Number of test cases = 3
網(wǎng)站欄目:創(chuàng)新互聯(lián)JUint教程:JUnit-API
網(wǎng)頁地址:http://www.dlmjj.cn/article/codosdc.html


咨詢
建站咨詢
