使用FEST-Swing测试GUI

 

在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了。 
下面新建一个被测试文件,代码如下:
 
这里我们就不详细说明代码含义了,大体的功能是点击按钮,可以将JTextField输入的文字显示在JLabel上面。需要注意的是,FEST-Swing是使用组件的name值来获取组件的,因此这个setName方法的调用是必不可少的。 
下面新建一个JUnit4 Test Case。首先需要有一个FrameFixture对象的属性。这里可以把FrameFixture理解成被测试的对象,因为我们想测试一个JFrame,所以使用FrameFixture。在FEST-Swing中,这些类与Swing的组件名字大体是一致的,只是后面多了一个Fixture。比如,JButton对应的类就是JButtonFixture。然后在@Before方法中对其进行实例化:
 
在@After方法中对其进行清理:
 
然后编写@Test方法:
 
尽管没有注释,我想这个代码已经很清楚了:首先获得frame上面的input,在其中输入Hello World!,然后点击copy按钮,这时show的文字要求是Hello World!这就是一段自说明的代码,很明白。这里就是像前面所说的,使用组件设定的name值来获取组件。 
这样就编写完成一个测试用例了,整个的代码如下:
 
下面运行这个测试用例,就可以看到执行情况……貌似还有动画效果,很漂亮。呵呵~~
 
其实这里也只是按照其 官方网站上面的Getting Started说明了FEST-Swing的简单用法,更多具体的使用方法请参考网站上面的使用手册。

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
web服务demo程序及扩展。 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值