JFCUnit

發表于:2007-05-25來源:作者:點擊數: 標簽:使得jfcunit實戰介紹
JFCUnit實戰 JFCUnit介紹 JFCUnit使得你能夠為 Java 偏移應用程序編寫測試例子。它為從用代碼打開的窗口上獲得句柄提供了支持;為在一個部件層次定位部件提供支持;為在部件中發起事件(例如按一個按鈕)以及以線程 安全 方式處理部件測試提供支持。JFCunit
JFCUnit實戰

JFCUnit介紹
JFCUnit使得你能夠為Java偏移應用程序編寫測試例子。它為從用代碼打開的窗口上獲得句柄提供了支持;為在一個部件層次定位部件提供支持;為在部件中發起事件(例如按一個按鈕)以及以線程安全方式處理部件測試提供支持。JFCunit的基本測試思路是:它提供了很多方法,可以用來模擬許多本應由傳統測試人員手工進行的觸發事件,如:點擊按鈕,給文本框輸入字符或數字,鼠標雙擊事件等,從而實現了測試的自動化。這一優點在需用戶輸入大量信息的界面測試中顯的尤為重要。實際上JFCUnit的測試用例十分像RobotJ中的腳本的編寫,使用熟練后對于自動化大批量測試十分有意義。
    更加詳細的介紹可以參考本人前面貼過的相關主題“ZT:JUNIT用于界面測試中的增強版JFCunit,建議用其進行界面單元測試”,這里不再敷訴。
使用示例
    以下是一個具體實例,測試對象是一個Jdialog,界面效果如下:
 
我們設計的測試目的是:
1、    檢查界面上是否存在各個設計組件;
2、    在用戶名和密碼為空的情況下,模擬點擊確定按鈕事件,是否能在原來的對話框上顯示一個錯誤提示框,提示框的標題為"Login Error";
即最后顯示效果如下:
 
以下是相關的測試對象代碼和測試用例代碼。
測試對象代碼:

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);
    }
  }
}

測試用例代碼:

import java.util.*;

import javax.swing.*;

import junit.extensions.jfcunit.*;
import junit.extensions.jfcunit.eventdata.*;
import junit.framework.*;
import org.apache.regexp.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author leiwei
 * @version 1.0
 */

public class LoginScreenTest
    extends JFCTestCase {

  private LoginScreen loginScreen = null;
  private TestHelper helper = null;

  public LoginScreenTest(String name) {
    super(name);
  }

  protected void setUp() throws Exception {
    super.setUp();
    helper = new JFCTestHelper();
    loginScreen = new LoginScreen("LoginScreenTest: " + getName());
    loginScreen.setVisible(true);
  }

  protected void tearDown() throws Exception {
    loginScreen = null;
    helper.cleanUp(this);
    super.tearDown();
  }


  public void testUI() {
    JDialog dialog;

    JButton exitButton = (JButton) helper.findNamedComponent("ExitButton",
        loginScreen, 0);
    assertNotNull("Could not find the Exit button", exitButton);

    JButton enterButton = (JButton) helper.findNamedComponent("EnterButton",
        loginScreen, 0);
    assertNotNull("Could not find the Enter button", enterButton);

    JTextField userNameField = (JTextField) helper.findNamedComponent(
        "LoginNameTextField", loginScreen, 0);
    assertNotNull("Could not find the userNameField", userNameField);

    assertEquals("Username field is empty", "", userNameField.getText());

    JTextField passwordField = (JTextField) helper.findNamedComponent(
        "PasswordTextField", loginScreen, 0);
    assertNotNull("Could not find the passwordField", passwordField);

    assertEquals("Password field is empty", "", passwordField.getText());

    try {
      helper.enterClickAndLeave(new MouseEventData(this, enterButton));
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }

    List showingDialogs = helper.getShowingDialogs(loginScreen);
    assertEquals("Number of dialogs showing is wrong", 2, showingDialogs.size());

    dialog = (JDialog) showingDialogs.get(1);
    assertEquals("Wrong dialog showing up", "Login Error", dialog.getTitle());

    helper.disposeWindow(dialog, this);
  }





  public static Test suite() {
    return new TestSuite(LoginScreenTest.class);
  }


  public void TestUI(){}

  public static void main(String args[]) {
    junit.textui.TestRunner.run(suite());

  }

}

小結
1、    JFCUnit的基本框架與原理與Junit一樣,其實從實現上是完全基于Junit,是Junit在GUI界面測試上的有效擴展。
2、    JFCUnit功能十分強大,擅長對象實例檢查,各種人機界面響應事件的模擬,便于功能性檢查。
3、    JFCUnit不善于界面布局,美觀方面的檢查,看來人工測試還是不可缺少的。
4、    JFCUnit中對對象進行測試的基礎是獲得測試對象實例,關鍵是find…Component系列API的應用,具體查找主要基于兩個方面,對象組件的“Name”屬性和在容器中的Index,而這兩種方式都不是很直觀,具體使用何種方式也很大程度上取決于開發人員的編碼,具體使用時有一定難度和局限性。
5、    JFCUnit還有很多深入用法,比如支持GUI測試的多線程方法,支持XML方式測試,以后有時間再與大家分享。
6、    由于相關資料比較缺乏,很多測試經驗完全是個人摸索得來,希望以后有人做更深入的研究。


相關資料
【1】    http://sourceforge.net/
【2】    http://epoxy.mrs.umn.edu/
【3】    《JFCunit 在單元測試中的應用》

原文轉自:http://www.anti-gravitydesign.com

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97