Java GUI Testing - JFCUnit Introduce

简介: Java GUI Testing - JFCUnit Introduce Background: JFCUnit is an extension to the popular testing framework JUnit.

Java GUI Testing - JFCUnit Introduce

Background:

JFCUnit is an extension to the popular testing framework JUnit. This document assumes you are familiar with the usage of JUnit. If not, visit the main JUnit website where there are a number of links to some excellent resources on the subject.

 

 

         Environment setup & JFCUnit installing…

Download below jars:  JUnit.jar 3.7 or greater, JFCUnit.jar, jakarta-regexp-1.5.jar 

Install JRE1.4 or greater & Eclipse IDE

 

         A Sample Java GUI & JFCUnit Test

1.         Deploy downloaded jars into your project class path, see following image

 

 

2.         create java GUI sample codes

package com.fish.ui;

import java.awt.BorderLayout;

import java.awt.FlowLayout;

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

 

public class NewTestViewImpl extends JDialog implements TestView {

 

    /**

     *

     */

    private static final long serialVersionUID = 1L;

 

    private JTextField nameField;

    private JButton submitButton;

    private JButton cancelButton;

    private JLabel nameLabel;

    private boolean proceed = false;

 

    public NewTestViewImpl(Object object) {

       // super(object);

       initComponents();

       layoutComponent();

    }

 

    private void initComponents() {

       this.nameField = new JTextField(15);

       this.submitButton = new JButton("OK");

       this.cancelButton = new JButton("Cancel");

       this.nameLabel = new JLabel("Test Name:");

    }

 

    private void layoutComponent() {

       JPanel topPane = new JPanel();

       topPane.add(nameLabel);

       topPane.add(nameField);

       JPanel buttonPane = new JPanel();

       buttonPane.setLayout(new FlowLayout());

       buttonPane.add(submitButton);

       buttonPane.add(cancelButton);

       getContentPane().add(topPane, BorderLayout.NORTH);

       getContentPane().add(buttonPane, BorderLayout.LINE_END);

       pack();

    }

   

 

    public String getTestName() {

       return nameField.getText();

    }

   

    public void setTestName(String testName) {

       this.nameField.setText(testName);

    }

 

    public void display() {

       this.setVisible(true);

    }

 

    public boolean proceed() {

       return proceed;

    }

 

    public boolean cancelled() {

       return !proceed;

    }

 

    public static void main(String[] args) {

       new NewTestViewImpl(null).setVisible(true);

    }

 

    public void setupJFCNames() {

       nameField.setName("testNameField");

       submitButton.setName("SubmitButton");

       cancelButton.setName("CancelButton");    

    }

 

    public void addListener(Listener listener) {

       submitButton.addActionListener(listener);

       cancelButton.addActionListener(listener);

    }

 

 

    public void closeView() {

   

    }

 

 

    public void openView() {

 

    }

}

 

3.         JFCUnit test codes

package com.fish.ui.test;

 

import javax.swing.JButton;

import javax.swing.JComponent;

import javax.swing.JTextField;

 

import com.fish.ui.NewTestViewImpl;

 

import junit.extensions.jfcunit.JFCTestCase;

import junit.extensions.jfcunit.JFCTestHelper;

import junit.extensions.jfcunit.TestHelper;

import junit.extensions.jfcunit.finder.NamedComponentFinder;

import junit.framework.Test;

import junit.framework.TestSuite;

 

public class NewTestViewImplTest extends JFCTestCase {

    private NewTestViewImpl newTestViewImpl = null;

    private TestHelper helper = null;

 

    public NewTestViewImplTest(String name) {

        super(name);

    }

 

    protected void setUp() throws Exception {

       super.setUp();

       helper = new JFCTestHelper();

       newTestViewImpl = new NewTestViewImpl(null);

       newTestViewImpl.setupJFCNames();

       newTestViewImpl.setVisible(true);

       System.out.println("setup test case");

    }

 

    protected void tearDown() throws Exception {

       newTestViewImpl = null;

       TestHelper.cleanUp(this);

       super.tearDown();

       System.out.println("setup test case");

 

    }

    public void testUI() {

       System.out.println("start testing ok button......");

       NamedComponentFinder finder = new NamedComponentFinder(

              JComponent.class, "SubmitButton");

       JButton submitButton = (JButton) finder.find(newTemplateViewImpl, 0);

       assertNotNull("Could not find the submit button", submitButton);

 

       finder = new NamedComponentFinder(

              JComponent.class, "CancelButton");

       JButton CancelButton = (JButton) finder.find(newTemplateViewImpl, 0);

       assertNotNull("Could not find the cancel button", CancelButton);

      

       finder = new NamedComponentFinder(

              JComponent.class, "testNameField");

       JTextField testNameField = (JTextField) finder.find(newTestViewImpl, 0);

       assertNotNull("Could not find the cancel button", templateNameField);

      

       newTestViewImpl.setTestName("mTestName");

       assertEquals(testNameField.getText(), "mTestName");

//     System.out.println(text);

       testNameField.setText("");

       assertEquals("Test field is not null","",newTestViewImpl.getTestName());   

      

    }

    public static Test suite() {

       return new TestSuite(NewTestViewImplTest.class);

    }

 

    public static void main(String args[]) {

       junit.textui.TestRunner.run(suite());

    }

 

}

4.         Run JFCUnit test case using eclipse plug-in Junit Runner to load testcase and run.

More Information:

http://jfcunit.sourceforge.net/

目录
相关文章
|
4月前
|
前端开发 Oracle Java
Java中的GUI应用开发技术选型
Java中的GUI应用开发技术选型
|
4月前
|
Java Apache Android开发
Java中的GUI界面设计与用户体验优化
Java中的GUI界面设计与用户体验优化
|
3月前
|
存储 数据可视化 Java
【Java】Java swing 民宿管理系统 GUI(源码+可视化界面)【独一无二】
【Java】Java swing 民宿管理系统 GUI(源码+可视化界面)【独一无二】
|
5月前
|
前端开发 Java 开发工具
Java GUI编程:跨平台应用的设计与开发
Java GUI编程:跨平台应用的设计与开发
|
4月前
|
前端开发 Java 开发工具
Java GUI编程:跨平台应用的设计与开发
Java GUI编程:跨平台应用的设计与开发
|
4月前
|
Java 数据安全/隐私保护 容器
Java详解:GUI容器组件 | 功能组件
Java详解:GUI容器组件 | 功能组件
|
4月前
|
Java 容器
Java详解:GUI图形用户界面设计—容器组件及面板布局方式
Java详解:GUI图形用户界面设计—容器组件及面板布局方式
|
6月前
|
存储 前端开发 Java
Java一分钟之-Java GUI设计原则与模式
本文介绍了Java GUI开发的核心设计原则和模式,包括分层架构(MVC)、组件复用、用户体验和代码示例。强调了MVC模式以提高代码可维护性,组件化设计以增强复用性,以及响应式和简洁界面以提升用户体验。同时,提出了常见问题的避免策略,如布局管理、资源释放和国际化支持,建议开发者遵循这些原则以提升GUI应用质量。
176 3
|
6月前
|
IDE Java Linux
Java一分钟之-JavaFX:构建桌面GUI应用
JavaFX是Java用于构建桌面应用的强大力量,提供丰富的UI组件、动画、媒体播放和跨平台能力。本文简要介绍JavaFX,讨论环境配置、布局混乱和事件处理等常见问题及其解决方案。通过学习官方文档、实践和使用IDE辅助,开发者能避免这些问题。示例代码展示了一个简单的JavaFX应用,展示如何创建UI、处理事件和构建布局。
297 1
|
5月前
|
IDE Java 开发工具
Java GUI Testing - JFCUnit Introduce
Java GUI Testing - JFCUnit Introduce
22 0