注册 | 登录 | 设为首页 | 加入收藏
您当前的位置:飞翔学院-IT中国 → 编程开发AJAX → 文章内容

对Ajax应用程序进行单元测试

作者:佚名 来源:不详 发布时间:2007-12-10 22:48:41

  清单 1 的代码在运行时发生了严重的错误:它无法按照 JUnit 和 GWT 的 GWTTestCase 进行测试。事实上,如果我试着为这段代码编写测试,从技术方面来说它可以运行,但是无法按照逻辑工作。考虑一下:您如何对这段代码进行验证?惟一可用于测试的 public 方法返回的是 void, 那么,您怎么能够验证其功能的正确性呢?

  如果我想以白盒方式验证这段代码,就必须分离业务逻辑和特定于用户界面的代码,这就需要进行重构。这本质上意味着把清单 1 中的代码分离到一个便于测试的独立方法中。但是这并非听上去那么简单。很明显组件挂钩是通过 onModuleLoad() 方法实现,但是如果我想强制其行为,可能 必须操纵某些用户界面(UI)组件。

  分解业务逻辑和 UI 代码

  第一步是为每个 UI 组件创建访问器方法,如清单 2 所示。按照该方式,我可以在需要时获取它们。

  清单 2. 向 UI 组件添加访问器方法使其可用public class WordModule implements EntryPoint {
 private Label label;
 private Button button;
 private TextBox textBox;
 private Label outputLabel;
 
 protected Button getButton() {
  if (this.button == null) {
  this.button = new Button("Submit");
  }
  return this.button;
 }
 protected Label getLabel() {
  if (this.label == null) {
  this.label = new Label("Word: ");
  }
  return this.label;
 }
 protected Label getOutputLabel() {
  if (this.outputLabel == null) {
  this.outputLabel = new Label();
  }
  return this.outputLabel;
 }
 protected TextBox getTextBox() {
  if (this.textBox == null) {
  this.textBox = new TextBox();
  this.textBox.setVisibleLength(20);
  }
  return this.textBox;
 }
}


更多内容请看PCdog.com--Ajax技术专题
上一页  [1] [2] [3] [4] [5] [6] [7] [8]  下一页


  • 打印文档
  • 推荐好友
  • 返回顶部
  • 增大字体
  • 减少字体
关于本站 | 工作机会 | 合作网站 | 广告服务 | 市场合作| 联系我们 | 抽奖活动
版权所有: 武汉威俊科技有限公司 Copyright 2005-2007 www.ITCNW.COM All rights reserved