Java单元测试进阶之如何进行界面测试(眼见为实)
2015-05-02 14:33
507 查看
一般的测试,尤其是逻辑处理代码的测试,JUnit就足够了。但是一些特殊的测试,譬如需要连接数据库, 使用浏览器,这些测试单单使用JUnit还不能够很好地完成任务,于是会偷懒的牛人又发明了DbUnit、HTTPUnit这些测试框架,使得重复劳动大大减少。
但是对于界面测试,到现在为止还没有一个框架能够使得界面测试完全地自动化起来。
根据马克思主义哲学原理,这是由事物的内因来决定的。由于界面的复杂性以及界面元素的不确定性,界面的测试只有在它实实在在地呈现在测试人员的眼里时,测试才能得出结果,接着测试才能继续下去。即使现在已经有了不少界面测试的框架,但是这些所谓的框架还是不能够称之为框架,只能称之为工具,因为使用这些工具仍然得做很多重复的工作,而且能够自动化测试的程度有限。
下文介绍的是进行界面测试的一种技术,或者说是一种方法,也或者说是一种思想。虽然我在前言部分声明不谈方法和思想,只谈技术,但在这里还是得谈一下方法和思想,实际上,在界面测试中,技术、思想、方法的概念是挺模糊的。相信不久的将来,当出现一个很牛的框架来进行界面测试时,这几个概念就会越来越清晰。
先来把被测界面代码写出来:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/************************************************************
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 项目名称:lhjTest
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 文件名称:CoolUiClass.java
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 文件描述:CoolUiClass.java
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 作者:Administrator
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 创建日期:2007-3-24
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 修改记录:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
***********************************************************/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package org.lhj.cool.uiunit;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.Dimension;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.GridBagConstraints;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.GridBagLayout;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.Insets;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.event.ActionEvent;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.event.ActionListener;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.swing.JButton;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.swing.JFrame;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.swing.JPanel;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.swing.JScrollPane;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.swing.JTextArea;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @author Administrator
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public class CoolUiClass extends JPanel
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private JTextArea msgTxt = new JTextArea(400, 320);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private JButton coolBtn = new JButton("Cool");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public CoolUiClass()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.setLayout(new GridBagLayout());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.setMinimumSize(new Dimension(400, 350));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.setPreferredSize(new Dimension(400, 350));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
coolBtn.setMinimumSize(new Dimension(80, 21));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
coolBtn.setPreferredSize(new Dimension(80, 21));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
JScrollPane scrollPane = new JScrollPane(msgTxt);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scrollPane.setMinimumSize(new Dimension(400, 320));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scrollPane.setPreferredSize(new Dimension(400, 320));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.add(scrollPane, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.add(coolBtn, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
coolBtn.addActionListener(new ActionListener()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void actionPerformed(ActionEvent arg0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
appendText();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
});
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void appendText()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
msgTxt.append("cool
");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* test the calss in main,use your eye to test.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param args
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static void main(String[] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
JFrame frm = new JFrame("CoolUiClass");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frm.getContentPane().add(new CoolUiClass());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frm.pack();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frm.setVisible(true);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
简单地运行以下main方法,界面如下:
这其实已经在测试界面了,我在main函数里头写的就是测试代码,对于界面显示的测试,就这几行代码就足够了。你这时候是不是觉得:这也忒容易了,没什么难度。但我得告诉你,这才是刚刚开始。
界面除了显示,更复杂的是要进行事件处理,每一个界面控件都有可能是事件源,一个事件得引起一系列的操作,而且是对于界面的操作。这就跟一般的函数的测试相差甚大了,一般来说,函数的测试就是输入一个参数,然后判断输出参数,输出参数无非就那几种情况,要么抛异常、要么返回空、要么返回具体值。而界面的事件处理一般会改变很多个控件的显示,而且控件处于不同的状态时改变的情况也不一定是一样的,所以要测试处理是否正确,得判断多个值,而且得根据控件之前的状态来判断当前显示是否正确。看到这,你似乎感觉无从下手了是吧。幸亏,界面测试有其方便性,我们可以直接拿眼睛判断界面上显示是否正确了,虽然这样的测试方法无法保证完全正确,但是却很高效。在界面测试中,拿眼睛进行判断比拿机器进行判断肯定效率要高的多。
如果紧紧是界面逻辑的测试,用眼睛来测(我称之为目测法)已经足够了,可以很高效地发现问题。但是如果界面上用到的数据是来自于其它地方(譬如说是后台),那么在main方法里还不能够写出完整的测试代码。这时候我们就需要一种方法来满足这种需求,其实下面的方法才是目测法的精髓:将与界面处理逻辑无关的代码抽出来,例如上面appendText(这里仅仅举个例子,appendText实际上包含了界面处理逻辑,实际上只需要把非界面处理逻辑代码抽出来,例如文件操作,访问后台,数据库操作这些情况就应该这样做)的形式,然后使用继承的方式,将非界面处理逻辑代码给替换成自己需要的数据(俗称假数据),这样一来,界面处理逻辑代码就可以得到很好的测试。见以下代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/************************************************************
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Project Name: lhjTest
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* File Name : CoolUiClassETest.java
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* File Desc : CoolUiClassETest.java
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Author : Administrator
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Create : 2007-3-24
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Modify:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
***********************************************************/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package org.lhj.cool.uiunit;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.swing.JFrame;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.lhj.cool.uiunit.CoolUiClass;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @author Administrator
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public class CoolUiClassETest extends CoolUiClass
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/* (non-Javadoc)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @see org.lhj.cool.uiunit.CoolUiClass#appendText()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@Override
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void appendText()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// call the super method twice
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
super.appendText();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
super.appendText();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* test the calss in main,use your eye to test.So this method calls Eye Test.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* the eyt test method's idea is extends the test calss and overrride somemethod so
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* that you can expect what you need.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param args
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static void main(String[] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
JFrame frm = new JFrame("CoolUiClassETest");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frm.getContentPane().add(new CoolUiClassETest());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frm.pack();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frm.setVisible(true);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
上面仅仅是个示例,实际的测试中要复杂得多,不过只要大家掌握了这个精髓,在编码就考虑这种测试,相信界面测试会成为一种乐趣。
但是对于界面测试,到现在为止还没有一个框架能够使得界面测试完全地自动化起来。
根据马克思主义哲学原理,这是由事物的内因来决定的。由于界面的复杂性以及界面元素的不确定性,界面的测试只有在它实实在在地呈现在测试人员的眼里时,测试才能得出结果,接着测试才能继续下去。即使现在已经有了不少界面测试的框架,但是这些所谓的框架还是不能够称之为框架,只能称之为工具,因为使用这些工具仍然得做很多重复的工作,而且能够自动化测试的程度有限。
下文介绍的是进行界面测试的一种技术,或者说是一种方法,也或者说是一种思想。虽然我在前言部分声明不谈方法和思想,只谈技术,但在这里还是得谈一下方法和思想,实际上,在界面测试中,技术、思想、方法的概念是挺模糊的。相信不久的将来,当出现一个很牛的框架来进行界面测试时,这几个概念就会越来越清晰。
先来把被测界面代码写出来:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/************************************************************
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 项目名称:lhjTest
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 文件名称:CoolUiClass.java
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 文件描述:CoolUiClass.java
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 作者:Administrator
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 创建日期:2007-3-24
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 修改记录:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
***********************************************************/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package org.lhj.cool.uiunit;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.Dimension;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.GridBagConstraints;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.GridBagLayout;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.Insets;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.event.ActionEvent;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.event.ActionListener;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.swing.JButton;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.swing.JFrame;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.swing.JPanel;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.swing.JScrollPane;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.swing.JTextArea;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @author Administrator
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public class CoolUiClass extends JPanel
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private JTextArea msgTxt = new JTextArea(400, 320);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private JButton coolBtn = new JButton("Cool");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public CoolUiClass()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.setLayout(new GridBagLayout());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.setMinimumSize(new Dimension(400, 350));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.setPreferredSize(new Dimension(400, 350));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
coolBtn.setMinimumSize(new Dimension(80, 21));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
coolBtn.setPreferredSize(new Dimension(80, 21));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
JScrollPane scrollPane = new JScrollPane(msgTxt);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scrollPane.setMinimumSize(new Dimension(400, 320));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scrollPane.setPreferredSize(new Dimension(400, 320));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.add(scrollPane, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.add(coolBtn, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
coolBtn.addActionListener(new ActionListener()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void actionPerformed(ActionEvent arg0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
appendText();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
});
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void appendText()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
msgTxt.append("cool
");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* test the calss in main,use your eye to test.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param args
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static void main(String[] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
JFrame frm = new JFrame("CoolUiClass");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frm.getContentPane().add(new CoolUiClass());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frm.pack();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frm.setVisible(true);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
简单地运行以下main方法,界面如下:
这其实已经在测试界面了,我在main函数里头写的就是测试代码,对于界面显示的测试,就这几行代码就足够了。你这时候是不是觉得:这也忒容易了,没什么难度。但我得告诉你,这才是刚刚开始。
界面除了显示,更复杂的是要进行事件处理,每一个界面控件都有可能是事件源,一个事件得引起一系列的操作,而且是对于界面的操作。这就跟一般的函数的测试相差甚大了,一般来说,函数的测试就是输入一个参数,然后判断输出参数,输出参数无非就那几种情况,要么抛异常、要么返回空、要么返回具体值。而界面的事件处理一般会改变很多个控件的显示,而且控件处于不同的状态时改变的情况也不一定是一样的,所以要测试处理是否正确,得判断多个值,而且得根据控件之前的状态来判断当前显示是否正确。看到这,你似乎感觉无从下手了是吧。幸亏,界面测试有其方便性,我们可以直接拿眼睛判断界面上显示是否正确了,虽然这样的测试方法无法保证完全正确,但是却很高效。在界面测试中,拿眼睛进行判断比拿机器进行判断肯定效率要高的多。
如果紧紧是界面逻辑的测试,用眼睛来测(我称之为目测法)已经足够了,可以很高效地发现问题。但是如果界面上用到的数据是来自于其它地方(譬如说是后台),那么在main方法里还不能够写出完整的测试代码。这时候我们就需要一种方法来满足这种需求,其实下面的方法才是目测法的精髓:将与界面处理逻辑无关的代码抽出来,例如上面appendText(这里仅仅举个例子,appendText实际上包含了界面处理逻辑,实际上只需要把非界面处理逻辑代码抽出来,例如文件操作,访问后台,数据库操作这些情况就应该这样做)的形式,然后使用继承的方式,将非界面处理逻辑代码给替换成自己需要的数据(俗称假数据),这样一来,界面处理逻辑代码就可以得到很好的测试。见以下代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/************************************************************
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Project Name: lhjTest
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* File Name : CoolUiClassETest.java
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* File Desc : CoolUiClassETest.java
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Author : Administrator
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Create : 2007-3-24
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* Modify:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
***********************************************************/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package org.lhj.cool.uiunit;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.swing.JFrame;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.lhj.cool.uiunit.CoolUiClass;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @author Administrator
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public class CoolUiClassETest extends CoolUiClass
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/* (non-Javadoc)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @see org.lhj.cool.uiunit.CoolUiClass#appendText()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@Override
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void appendText()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// call the super method twice
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
super.appendText();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
super.appendText();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* test the calss in main,use your eye to test.So this method calls Eye Test.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* the eyt test method's idea is extends the test calss and overrride somemethod so
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* that you can expect what you need.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param args
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static void main(String[] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
JFrame frm = new JFrame("CoolUiClassETest");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frm.getContentPane().add(new CoolUiClassETest());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frm.pack();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
frm.setVisible(true);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
上面仅仅是个示例,实际的测试中要复杂得多,不过只要大家掌握了这个精髓,在编码就考虑这种测试,相信界面测试会成为一种乐趣。
相关文章推荐
- Java单元测试进阶之如何进行界面测试(也让界面测试自动化起来)
- Java单元测试进阶之单元测试框架的出现(初现雏形)
- Java单元测试进阶之单元测试框架的出现(框架兴起)
- Java单元测试进阶之单元测试利器JUnit(测试自动化)
- Java单元测试进阶之如何打桩(用easymock轻松打桩)
- Java单元测试进阶之如何打桩(扫除打桩的最后一道坎-使用easymockclassextension打桩类)
- 如何在java中使用sikuli进行自动化测试
- 使用 Visual Studio Team Test 进行单元测试和java中的测试
- 使用 Visual Studio Team Test 进行单元测试和java中的测试
- XCode 内置了 OCUnit 单元测试框架,但目前最好用的测试框架应该是 GHUnit。通过 GHUnit + OCMock 组合,我们可以在 iOS 下进行较强大的单元测试功能。本文将演示如何在
- 如何在依赖aar的情况下进行Java单元测试
- ionic之如何应用karma进行单元测试(2)-测试controller和service
- 如何在linux命令行无界面下,使用selenium进行自动化测试
- java如何使用JUnit进行单元测试
- 测试系列之一----如何进行单元测试 (转)
- 测试系列之二----如何进行单元测试
- 刚刚搜素到的 vc 进行单元 测试如何使用VC6进行单元测试
- 使用 ConTest 进行多线程单元测试 - 为什么并行测试很困难以及如何使用 ConTest 辅助测试
- 用 ConTest 进行多线程单元测试 - 为什么并行测试很困难以及如何使用 ConTest 辅助测试
- ASP.NET MVC单元测试时如何对含有ModelState.IsValid的Action进行测试