Swing自动化测试的工具FEST-Swing

         FEST-Swing是一个用于Swing GUI应用程序功能测试的java开源类库。支持模拟用户交互(鼠标操作和键盘输入)。支持JDK中的所有Swing组件。提供简洁、强大的API来创建和维护GUI功能测试。支持在HTML测试报告中嵌入GUI测试失败的截屏。能够与JUnit或TestNG一起使用。
关于FEST-Swing的官方网站:

http://fest.easytesting.org/

Google的位置:

http://code.google.com/p/fest/

在Java中最常用的单元测试工具要算JUnit了。FEST-Swing是一个能够与JUnit集成的GUI测试框架。使用FEST-Swing可以更方便的对Swing进行一系列的测试。下面就说一下如何使用FEST-Swing进行测试。
 
首先到 http://code.google.com/p/fest/downloads/list上面下载最新的FEST-Swing文件,一般文件名应该是fest-swing-x.x.zip。下载完成之后解压
注意,需要将fest-swing-1.0.jar以及lib下面的所有文件都添加到工程路径下,同时需要添加JUnit支持。这里我们使用JUnit4,添加完成之后即可使用FEST-Swing了。
下面新建一个被测试文件,代码如下:
package com.easyway.swing;

import java.awt.BorderLayout; 
import java.awt.Container; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.Box; 
import javax.swing.BoxLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JTextField; 
/**
 * SWing测试的主界面
 * @author longgangbai
 *
 */
public class CustomFrame extends JFrame { 
  /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public CustomFrame() { 
	    setTitle("My Frame"); 
	    setSize(400, 300); 
	    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
	     
	    Container content = getContentPane(); 
	    Box vbox = new Box(BoxLayout.Y_AXIS); 
	    content.add(vbox, BorderLayout.CENTER); 
	     
	    final JLabel showTextLabel = new JLabel(" "); 
	    showTextLabel.setName("show"); 
	    vbox.add(showTextLabel); 
	    final JTextField input = new JTextField(); 
	    input.setName("input"); 
	    vbox.add(input); 
	    JButton button = new JButton("copy");
	    button.setName("copy"); 
	    button.addActionListener(new ActionListener() { 
	
	      @Override 
	      public void actionPerformed(ActionEvent e) { 
	        showTextLabel.setText(input.getText()); 
	      } 
	        
	    }); 
	    vbox.add(button); 
	  } 
}
 
这里我们就不详细说明代码含义了,大体的功能是点击按钮,可以将JTextField输入的文字显示在JLabel上面。需要注意的是,FEST-Swing是使用组件的name值来获取组件的,因此这个setName方法的调用是必不可少的。
 
下面新建一个JUnit4 Test Case。首先需要有一个FrameFixture对象的属性。这里可以把FrameFixture理解成被测试的对象,因为我们想测试一个JFrame,所以使用FrameFixture。在FEST-Swing中,这些类与Swing的组件名字大体是一致的,只是后面多了一个Fixture。比如,JButton对应的类就是JButtonFixture。然后在@Before方法中对其进行实例化:
测试代码:
package com.easyway.swing.test;

import org.fest.swing.fixture.FrameFixture;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.easyway.swing.CustomFrame;
/**
 *     FEST-Swing是一个用于Swing GUI应用程序功能测试的java开源类库。支持模拟用户交互(鼠标操作和键盘输入)。
 *     支持JDK中的所有Swing组件。提供简洁、强大的API来创建和维护GUI功能测试。支持在HTML测试报告中嵌入GUI测试失败的截屏。
 *     能够与JUnit或TestNG一起使用。
 * @author longgangbai
 *
 */
public class CustomFrameTest { 

  private FrameFixture frame; 
  /**
   * 创建主窗体对象
   */
  @Before 
  public void setUp() { 
    frame = new FrameFixture(new CustomFrame()); 
    frame.show(); 
  } 

  /**
   * 在@After方法中对其进行清理:
   */
  @After 
  public void tearDown() { 
    frame.cleanUp(); 
  } 

  @Test 
  public void testCopyTextToLabel() { 
    frame.textBox("input").enterText("Hello World!"); 
    frame.button("copy").click(); 
    frame.label("show").requireText("Hello World!"); 
  } 
} 
 备注:需要在classpath中添加FEST-Swing中lib的jar文件和fest-swing的jar文件。
 
 
以下摘自:

Fluent Assertions

This module provides a fluent interface for assertions. FEST's assertions are incredibly easy to write: just type "assertThat" followed the actual value and a dot, and any Java IDE will show you all the assertions available for the type of the given object to verify. No more confusion about the order of the "expected" and "actual" values. Our assertions are very readable as well: they read very close to plain English, making it easier for non-technical people to read test code.

Here are some examples:

int removed = employees.removeFired();
assertThat(removed).isZero();
  List<Employee> newEmployees = employees.hired(TODAY);
assertThat(newEmployees).hasSize(6)
                        .contains(frodo, sam);

assertThat(yoda).isInstanceOf(Jedi.class)
                .isEqualTo(foundJedi)
                .isNotEqualTo(foundSith);

We currently have two branches of FEST-Assert. The 1.x branch is the stable one that is not longer under development. The 2.x branch, currently under development, is a brand-new FEST-Assert that provides a better fluent interface and better extensibility options.

Fluent Reflection

This module provides an intuitive, compact and type-safe fluent API for Java reflection. It makes Java reflection tremendously easy to use: no more casting, checked exceptions, PriviledgedActions or calls to setAccessible. FEST's reflection module can even overcome the limitations of generics and type erasure.

Person person = constructor().withParameterTypes(String.class)
                             .in(Person.class)
                             .newInstance("Yoda");

method("setName").withParameterTypes(String.class)
                 .in(person)
                 .invoke("Luke");

For more details, please visit the FEST-Reflect github project.

EasyMock Template

Our EasyMock template eliminates code duplication (repetitive calls to replay and verify) and clearly separates expectations from code to test.

@Testpublicvoid shouldUpdateEmployee(){
  newEasyMockTemplate(mockEmployeeDao){
    protectedvoid expectations(){
      mockEmployeeDAO.update(employee);
    }

    protectedvoid codeToTest(){
      employeeBO.updateEmployee(employee);
    }
  }.run();
}

Note: We are no longer developing the EasyMock template. We are currently replacing EasyMock with Mockito in our own tests. Since, in our opinion, Mockito's API is really nice and compact, it does not require a template.

你可能感兴趣的:(SWing自动化测试工具,FEST-Swing)