一.
介紹
JFCUnit
是一款在
JUnit
基礎上針對
Swing GUI
擴展的單元測試工具。在同一個應用程序中,我們可以通過組件發現方法查找到組件,模擬用戶動作觸發組件事件來提供測試驅動,通過斷言驗證組件狀態是否正確。
優點:
Ø
繼承了
JUnit
,具有
JUnit
進行單元測試的所有優點;
Ø
提供了一系列
GUI
組件發現方法及用戶動作模擬方法。
在讀下面的內容之前,您可以先下載“
JFCUnit Example
”。這是一個完整的
eclipse
工程,首先運行其中的一個例子,可以使您對
JFCUnit
有一個感官上的了解。
二.
JFCUnit API
說明:此處僅是簡單介紹,具體使用可以參見“
JFCUnit Example
”。
1.
GUI
組件發現類
a.
通用命名組件發現類:
NamedComponentFinder
(java.lang.Class cls, java.lang.String name)
這個類使用最多,也最容易,但需要在我們的GUI程序中將組件命名(如
cancelBtn.setName(“cancelBtn”)
)
。
示例代碼
:
NamedComponentFindercancelBtnFinder = new NamedComponentFinder(JButton.class, "cancelBtn");
JButton cancelBtn = (JButton) cancelBtnFinder.find();
assertNotNull("cancelBtn not found!", cancelBtn);
assertEquals(true, cancelBtn.isEnabled());
b.
其它組件發現類:
這一些類主要是針對一些特定的
GUI
組件提供了一些特殊的查找方法。主要有:
DialogFinder
FrameFinder
JLabelFinder
等等。具體使用可以查找
API
文檔,在
junit.extensions.jfcunit
下的
doc
文檔中。
示例代碼
:
FrameFinder frameFinder = new FrameFinder("FrameDemo");
//
字符串為
Frame
標題
List frames = frameFinder.findAll();
assertEquals("frames size wrong",1,frames.size());
JFrame frame = (JFrame)frames.get(0);
assertNotNull("frame is null !",frame);
2.
用戶動作模擬類
這些類主要有:
AbstractMouseEventData
DragEventData
JComboBoxMouseEventData
JListMouseEventData
JTabbedPaneMouseEventData
等等。這些類的使用大同小異。在
“
JFCUnit Example
”也有詳細的使用例子。下面以表格為例:
finder.setName(
"table"
);
JTable table = (JTable)finder.find(frame, 0);
//
從
Frame
中查找到表格
assertNotNull(
"table not found !"
,table);
//
模擬雙擊單元格
JTableMouseEventDatatableEvent=
new
JTableMouseEventData(
this
,table,1,2,2);
helper
.enterClickAndLeave(tableEvent);
this
.flushAWT();
//
執行事件派發隊列中的線程
,
保讓事件已經被響應。
//
模擬改變單元格的文本值
helper
.enterClickAndLeave(tableEvent);
this
.flushAWT();
helper
.sendKeyAction(
new
KeyEventData(
this
,tableEvent.getComponent(),KeyEvent.
VK_DELETE
));
helper
.sendString(
new
StringEventData(
this
,tableEvent.getComponent(),
""
));
helper
.sendString(
new
StringEventData(
this
,tableEvent.getComponent(),
"wukaichun"
));
this
.flushAWT();
//
模擬多選表格行
JTableMouseEventDatasrcEvent =
new
JTableMouseEventData(
this
,table,1,2,1);
JTableMouseEventDatasinkEvent =
new
JTableMouseEventData(
this
,table,2,2,1);
helper
.enterDragAndLeave(
new
DragEventData(
this
,srcEvent,sinkEvent));
this
.flushAWT();
三.
配置
對于實際
GUI
項目,往往需要按界面分別進行測試。為了多個測試用例共用測試環境(不必每次都啟動項目),可以將被測工程與
JUnit
一起啟動。而后在
JUnit
的
swingUI
中選取用例執行。
如:
package
person.jfcunit.test;
import
junit.swingui.TestRunner;
public
class
MainTest {
public
static
void
main(String[] args) {
LoginScreen.main(
new
String[]{});
//
啟動被測
GUI
程序
TestRunner.main(
new
String[]{});
//
啟動
JUnit
}
}
注意:
3.
啟動配置中除主函數變為
MainTest
外,其它配置與被測工程一樣;
4.
如果使用
JUnit swingui
測試界面,為了保證所有的組件都為同一加載器加載,需要將被測類改為默認加載器加載。
a.
找到文件
excluded.properties
,在
junit.jar
的
junit.runner
下;
b.
修改
excluded.properties
,將被測試類添加到
excluded
中,如下:
#
# The list of excluded package paths for the TestCaseClassLoader
#
excluded.0=sun.*
excluded.1=com.sun.*
excluded.2=org.omg.*
excluded.3=javax.*
excluded.4=sunw.*
excluded.5=java.*
excluded.6=org.w3c.dom.*
excluded.7=org.xml.sax.*
excluded.8=net.jini.*
excluded.9=org.apache.commons.logging.*
excluded.9=person.*
在這里列出的包將不使用
TestCaseClassLoader
加載,而是由默認加載器加載。如最后一行“
excluded.9=person.*
”,我們將
person
包中的類排除在
TestCaseClassLoader
加載之外。
這個時候,運行
MainTest
,您就可以對您的項目進行測試了。
四.
結束語
寫這篇文章的啟示是對當前公司的
GUI
網管進行單元測試時的總結。
JFCUnit
有一定的局限性,它只能針對于擴展了
Component
的
GUI
組件進行測試。對于
ilog
這類直接由
Object
繼承來的
GUI
則需要使用者自己擴展。但其簡單和易用性應該能滿足大多數實際項目的了。
在這里共享出來,希望對使用
Swing
的同仁能有點幫助。文章中有不明白或不正確之處,請不吝賜教。謝謝!
Email:wukeichun@163.com
。
五.
參考資料
JFCUnit
測試
GUI
的一個實例(配置篇)
可以了解在
eclipse
下
JFCUnit
的安裝及配置。
自作聰明的
junit.swingui.TestRunner
其中解釋了使用
swing ui
界面時的類加載器問題,值得一看。
posted on 2006-10-19 20:03
wukaichun 閱讀(2849)
評論(5) 編輯 收藏 所屬分類:
test