JFCUnit介紹
JFCUnit使得你能夠?yàn)镴ava偏移應(yīng)用程序編寫測(cè)試?yán)。它為從用代碼打開的窗口上獲得句柄提供了支持;為在一個(gè)部件層次定位部件提供支持;為在部件中發(fā)起事件(例如按一個(gè)按鈕)以及以線程安全方式處理部件測(cè)試提供支持。JFCunit的基本測(cè)試思路是:它提供了很多方法,可以用來模擬許多本應(yīng)由傳統(tǒng)測(cè)試人員手工進(jìn)行的觸發(fā)事件,如:點(diǎn)擊按鈕,給文本框輸入字符或數(shù)字,鼠標(biāo)雙擊事件等,從而實(shí)現(xiàn)了測(cè)試的自動(dòng)化。這一優(yōu)點(diǎn)在需用戶輸入大量信息的界面測(cè)試中顯的尤為重要。實(shí)際上JFCUnit的測(cè)試用例十分像RobotJ中的腳本的編寫,使用熟練后對(duì)于自動(dòng)化大批量測(cè)試十分有意義。
更加詳細(xì)的介紹可以參考本人前面貼過的相關(guān)主題“ZT:JUNIT用于界面測(cè)試中的增強(qiáng)版JFCunit,建議用其進(jìn)行界面單元測(cè)試”,這里不再敷訴。
使用示例
以下是一個(gè)具體實(shí)例,測(cè)試對(duì)象是一個(gè)Jdialog,界面效果如下:
我們?cè)O(shè)計(jì)的測(cè)試目的是:
1、 檢查界面上是否存在各個(gè)設(shè)計(jì)組件;
2、 在用戶名和密碼為空的情況下,模擬點(diǎn)擊確定按鈕事件,是否能在原來的對(duì)話框上顯示一個(gè)錯(cuò)誤提示框,提示框的標(biāo)題為"Login Error";
即后顯示效果如下:
以下是相關(guān)的測(cè)試對(duì)象代碼和測(cè)試用例代碼。
測(cè)試對(duì)象代碼:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import com.borland.jbcl.layout.*;
import java.awt.BorderLayout;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author leiwei
* @version 1.0
*/
public class LoginScreen extends JDialog{
JPanel jPanel1 = new JPanel();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField LoginNameTextField = new JTextField();
JButton EnterButton = new JButton();
JButton ExitButton = new JButton();
BorderLayout borderLayout1 = new BorderLayout();
JPasswordField PasswordTextField = new JPasswordField();
String title ="";
private GridBagLayout gridBagLayout1 = new GridBagLayout();
public LoginScreen(String ititle) {
try {
jbInit();
super.setTitle(ititle);
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setName("loginScreen");
this.setTitle(title);
this.setBounds(300,300,300,300);
jLabel2.setFont(new java.awt.Font("Dialog", 0, 13));
jLabel2.setText("密 碼:");
jLabel1.setFont(new java.awt.Font("Dialog", 0, 13));
jLabel1.setText("用戶名:");
jPanel1.setLayout(gridBagLayout1);
this.getContentPane().setLayout(borderLayout1);
EnterButton.setFont(new java.awt.Font("Dialog", 0, 12));
EnterButton.setText("確定");
EnterButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
EnterButton_actionPerformed(e);
}
});
ExitButton.setFont(new java.awt.Font("Dialog", 0, 12));
ExitButton.setText("取消");
ExitButton.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e) {
ExitButton_actionPerformed(e);
}
});
LoginNameTextField.setText("");
PasswordTextField.setText("");
jPanel1.add(jLabel1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(52, 49, 0, 0), 0, 0));
jPanel1.add(LoginNameTextField, new GridBagConstraints(1, 0, 2, 1, 1.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(52, 7, 0, 73), 214, 0));
jPanel1.add(PasswordTextField, new GridBagConstraints(1, 1, 2, 1, 1.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(17, 7, 0, 73), 214, 0));
jPanel1.add(jLabel2, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(19, 49, 0, 7), 0, 0));
jPanel1.add(EnterButton, new GridBagConstraints(0, 2, 2, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(35, 88, 62, 0), 12, 0));
jPanel1.add(ExitButton, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(38, 91, 62, 73), 13, 0));
this.getContentPane().add(jPanel1, BorderLayout.CENTER,0);
this.pack();
ExitButton.setName("ExitButton");
EnterButton.setName("EnterButton");
LoginNameTextField.setName("LoginNameTextField");
PasswordTextField.setName("PasswordTextField");
}
public void show(){
super.show();
}
public static void main(String args[]){
JDialog dialog = new LoginScreen("loginScreen");
dialog.show();
}
void ExitButton_actionPerformed(ActionEvent e) {
System.exit(0);
}
void EnterButton_actionPerformed(ActionEvent e) {
if(this.PasswordTextField.getPassword().length == 0){
javax.swing.JOptionPane.showMessageDialog(this,"Login Error","Login Error",JOptionPane.ERROR_MESSAGE);
}
}
}