Java GUI Testing - JFCUnit Introduce
来源:互联网 发布:it初学者 编辑:程序博客网 时间:2024/05/16 16:55
Java GUI Testing - JFCUnit Introduce
Background:
JFCUnit is an extension to the popular testing frameworkJUnit. 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;
publicclass NewTestViewImplextends JDialogimplements TestView {
/**
*
*/
privatestaticfinallongserialVersionUID = 1L;
private JTextFieldnameField;
private JButtonsubmitButton;
private JButtoncancelButton;
private JLabelnameLabel;
privatebooleanproceed =false;
public NewTestViewImpl(Object object) {
// super(object);
initComponents();
layoutComponent();
}
privatevoid initComponents() {
this.nameField = new JTextField(15);
this.submitButton = new JButton("OK");
this.cancelButton = new JButton("Cancel");
this.nameLabel = new JLabel("Test Name:");
}
privatevoid 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() {
returnnameField.getText();
}
publicvoid setTestName(String testName) {
this.nameField.setText(testName);
}
publicvoid display() {
this.setVisible(true);
}
publicboolean proceed() {
returnproceed;
}
publicboolean cancelled() {
return !proceed;
}
publicstaticvoid main(String[] args) {
new NewTestViewImpl(null).setVisible(true);
}
publicvoid setupJFCNames() {
nameField.setName("testNameField");
submitButton.setName("SubmitButton");
cancelButton.setName("CancelButton");
}
publicvoid addListener(Listener listener) {
submitButton.addActionListener(listener);
cancelButton.addActionListener(listener);
}
publicvoid closeView() {
}
publicvoid 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;
publicclass NewTestViewImplTestextends JFCTestCase {
private NewTestViewImplnewTestViewImpl =null;
private TestHelperhelper =null;
public NewTestViewImplTest(String name) {
super(name);
}
protectedvoid setUp()throws Exception {
super.setUp();
helper =new JFCTestHelper();
newTestViewImpl =new NewTestViewImpl(null);
newTestViewImpl.setupJFCNames();
newTestViewImpl.setVisible(true);
System.out.println("setup test case");
}
protectedvoid tearDown()throws Exception {
newTestViewImpl =null;
TestHelper.cleanUp(this);
super.tearDown();
System.out.println("setup test case");
}
publicvoid 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());
}
publicstatic Test suite() {
returnnew TestSuite(NewTestViewImplTest.class);
}
publicstaticvoid 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/
- Java GUI Testing - JFCUnit Introduce
- jfcUnit: 测试Java GUI Swing
- Java Simple Introduce
- jfcUnit【转】
- Scripted GUI Testing with Ruby
- introduce
- Introduce
- introduce
- Introduce to Java Programming:NOTE
- LintCode Introduce Java & Coding Interview
- parallel programming in java introduce
- Effective GUI Test Automation: Developing an Automated GUI Testing Tool
- java 代码细节(Introduce Explaining Variable)
- Java GUI
- Java GUI
- Java GUI
- Java GUI
- java GUI
- JAVA实现AES加密
- 决策
- linux gcc 的编译过程 详解
- ui li 横向排列 换行问题
- asp.net回调更新gridview
- Java GUI Testing - JFCUnit Introduce
- Qt Designer 的插件的编写
- 聚簇索引与非聚簇索引的区别以及SQL Server查询优化技术
- SQL优化-索引、查询优化及分页算法方案
- TEKLA 15.0 PLUGINS DLL 编译手册
- 评原百度CTO李一男的经典语录
- 蛇形矩阵和远程注入
- 高效SQL查询之索引(I)
- JAVA培训题库-AJAX