1. 匯入junit jar包;
2. 在專案目錄下新建乙個source fold(一般命名為 test);
3. 新建乙個測試類(一般命名規則為:被測試類名+test,例如: hellowordtest)繼承junit.framework.testcase類;
4. 編寫測試方法(方法名必須以test+用例方法名稱);
如下:junit被用來測試**,並且它是由能夠測試不同條件的斷言方法(assertion method)組成,常用斷言方法如下:
測試a是否等於b(a和b是原始型別數值(primitive value)或者必須為實現比較而具有equal方法)
測試a是否為false(假),a是乙個boolean數值。
測試a是否非空,a是乙個物件或者null。
測試a和b是否沒有都引用同乙個物件。
測試a是否為null,a是乙個物件或者null。
測試a和b是否都引用同乙個物件。
測試a是否為true(真),a是乙個boolean數值。
我們使用這些方法來構建junit測試。當執行乙個junit應用程式時,它開啟自己的檢視(view)來立即指示哪個測試通過了,哪個測試失敗了。
被測試類:
測試類:
注:執行testrunner.run(hellowordtest.class)後會出現:
或time上的小點表示測試個數,如果測試通過則顯示ok。否則在小點的後邊標上f,表示該測試失敗。
綜上所敘:編寫測試類的基本步驟:
1>;擴充套件testcase類;
2>;覆蓋runtest()方法(可選);
3>;寫一些test***xx()方法;
1.匯入junit 4和hamcrest-all jar包;
2.assertthat:
基本語法: assertthat( [value], [matcher statement] );
value 是接下來想要測試的變數值;
matcher statement 是使用 hamcrest 匹配符來表達的對前面變數所期望的值的宣告,如果 value 值與 matcher statement 所表達的期望值相符,則測試成功,否則測試失敗。
匹配符表明如果接下來的所有條件必須都成立測試才通過,相當於「與」(&&)
assertthat( testednumber, allof( greaterthan(8), lessthan(16) ) );
匹配符表明如果接下來的所有條件只要有乙個成立則測試通過,相當於「或」(||)
assertthat( testednumber, anyof( greaterthan(16), lessthan(8) ) );
匹配符表明無論什麼條件,永遠為true
assertthat( testednumber, anything() );
匹配符表明如果前面待測的object等於後面給出的object,則測試通過
assertthat( testedstring, is( "developerworks" ) );
匹配符和is匹配符正好相反,表明如果前面待測的object不等於後面給出的object,則測試通過
assertthat( testedstring, not( "developerworks" ) );
匹配符表明如果測試的字串testedstring包含子字串"developerworks"則測試通過
assertthat( testedstring, containsstring( "developerworks" ) );
匹配符表明如果測試的字串testedstring以子字串"developerworks"結尾則測試通過
assertthat( testedstring, endswith( "developerworks" ) );
匹配符表明如果測試的字串testedstring以子字串"developerworks"開始則測試通過
assertthat( testedstring, startswith( "developerworks" ) );
匹配符表明如果測試的testedvalue等於expectedvalue則測試通過,equalto可以測試數值之間,字串之間和物件之間是否相等,相當於object的equals方法
assertthat( testedvalue, equalto( expectedvalue ) );
匹配符表明如果測試的字串testedstring在忽略大小寫的情況下等於 "developerworks"則測試通過
assertthat( testedstring, equaltoignoringcase( "developerworks" ) );
匹配符表明如果測試的字串testedstring在忽略頭尾的任意個空格的情況下等於"developerworks"則測試通過,注意:字串中的空格不能被忽略
assertthat( testedstring, equaltoignoringwhitespace( "developerworks" ) );
匹配符表明如果所測試的浮點型數testeddouble在20.0±0.5範圍之內則測試通過
assertthat( testeddouble, closeto( 20.0, 0.5 ) );
匹配符表明如果所測試的數值testednumber大於16.0則測試通過
assertthat( testednumber, greaterthan(16.0) );
匹配符表明如果所測試的數值testednumber小於16.0則測試通過
assertthat( testednumber, lessthan (16.0) );
匹配符表明如果所測試的數值testednumber大於等於16.0則測試通過
assertthat( testednumber, greaterthanorequalto (16.0) );
匹配符表明如果所測試的數值testednumber小於等於16.0則測試通過
assertthat( testednumber, lessthanorequalto (16.0) );
匹配符表明如果測試的map物件mapobject含有乙個鍵值為"key"對應元素值為"value"的entry項則測試通過
assertthat( mapobject, hasentry( "key", "value" ) );
匹配符表明如果測試的迭代物件iterableobject含有元素「element」項則測試通過
assertthat( iterableobject, hasitem ( "element" ) );
匹配符表明如果測試的map物件mapobject含有鍵值「key」則測試通過
assertthat( mapobject, haskey ( "key" ) );
匹配符表明如果測試的map物件mapobject含有元素值「value」則測試通過
assertthat( mapobject, hasvalue ( "key" ) );
表示「不等於」
assertthat( something, not( equalto( "developer" ) ) );
表示「不包含子字串」
assertthat( something, not( containsstring( "works" ) ) );
表示「包含任何乙個子字串」
assertthat(something, anyof(containsstring("developer"), containsstring("works")));
被測試類:
測試類:
免費共享資源,從我做起!
myeclipse使用教程
1.myeclipse是一款功能強大的j2ee ide與web開發工具。本文主要介紹如何利用myeclipse寫程式,並以簡單的例子展示具體步驟。2.step 1 選擇file new 在這邊,可以看到有j a project或者是web project,根據需要選擇乙個,然後選擇next。本文以建...
myeclipse快捷鍵的使用
注釋單行 ctrl 多行多行注釋 ctrl shift 取消注釋ctrl shift 檢視方法說明 f2 複製行 ctrl alt 向下鍵 有些不能用 宣告變數 ctrl 2,l 直接回車到下一行 shift enter 通過ctrl t 可以檢視類的層次結構 通過ctrl shift t 可以開啟...
MyEclipse中測試連線Mysql
1 開啟myeclipse 2 選擇選單 window open perspective myeclipse databaseexplorer,點選ok,在左邊開啟db brower視窗。3 在db brower視窗內任意空白處擊右鍵,選擇new命令,彈出database driver視窗 4 本視...