Unit and Integration Testing for GWT Applications翻译,持续更新中.欢迎指正
时间:2010-06-23
来源:互联网
本帖最后由 surpass_li 于 2010-06-23 15:12 编辑
源文http://www.infoq.com/articles/gwt_unit_testing
Unit and Integration Testing for GWT Applications
关于GWT应用的单元测试与集成测试.
GWT is a framework developed by Google for building AJAX enabled web applications using the Java programming language. It comprises:
GWT是一个Google用来使用java程序语言 构建AJAX web应用的开发框架,包括:
1. An API for creating GUI (similar to Swing), which manipulates the web browser's Document Object Model (DOM).
1. 一个创建GUI(类似于Swing)的API,使用web浏览文档对象(DOM)
2. A Java-to-JavaScript compiler.
2.一个Java 到 JavaScript编译器.
3. An environment for running and debugging GWT applications.
3.一个运行和调试GWT 应用的环境.
This approach offers some advantages:
提供一些优点:
* Assuming you know Java, there is no need to learn a new programming language.
* 如果你懂得Java,这将不需要你学习新的编程语言.
* You can build an AJAX enabled web application without writing JavaScript or HTML.
* 你可以构建一个web应用使用AJAX不需要写JavaScript or HTML.
* Everything is written in Java, so Java's advanced development tools, including debugging and re-factoring support, are available.
* 一切都是通过Java,那Java高级开发工具,可得到的包括调试和重复工厂化支持.
* GWT shields the developer from browser idiosyncrasies
* GWT保护开发者浏览行为
Finally, since everything is in Java (even the View part of the MVC pattern), we should be able to create UI tests easily. This article explores some approaches.
最后,因为所有事都在Java(even the View part of the MVC pattern),我们能很容易的测试UI.本文近一步探讨.
1. First attempt
1.首先尝试
1.1 GWT using TDD
1.1 GWT采用TDD(测试驱动开发)
We will start with a simple example: we want to display a text entry field, a button and a label. When we click on the button, the text field content entered by the user is placed in the label. The corresponding test (in JUnit 4) will be:
我们装开始一个简单的例子:想显示一个文本框.一个按钮和一个标签.当用户点按钮时文本框内容放到标签中.相应的(JUnit 4)测试如下:
1 @Test
2 public void testClick() {
3 GwtExample view = new GwtExample();
4 Assert.assertNull(view.getLabel().getText());
5 view.getTextBox().setText("my text");
6 // creation of a basic "click" event
7 NativeEvent event = Document.get().createClickEvent(0, 0, 0, 0, 0, false, false, false, false); // dispatch de l'évènement DomEvent.fireNativeEvent(event, view.getButton());
8 Assert.assertEquals("my text", view.getLabel().getText());
9 }
Then, we can write the corresponding View code:
然后我们写相应的展示代码:
1 public class GwtExample extends Composite {
2 private Label label;
3 private TextBox textBox;
4 private Button button;
5
6 public GwtExample() {
7 FlowPanel fp = new FlowPanel();
8 textBox = new TextBox();
9 fp.add(textBox);
10 button = new Button("validate");
11 fp.add(button);
12 button.addClickHandler(new ClickHandler() {
13
14 public void onClick(ClickEvent event) {
15 label.setText(textBox.getText());
16
17 }
18 });
19 label = new Label("init");
20 fp.add(label);
21 initWidget(fp);
22 }
23
24 public Label getLabel() { return label; }
25 public TextBox getTextBox() { return textBox; }
26 public Button getButton() { return button; }
27 }
Finally, we launch the preceding JUnit test:
最后我们运行之前的单元测试:
1 java.lang.ExceptionInInitializerError ...
2 Caused by: java.lang.UnsupportedOperationException: ERROR: GWT.create() is only usable in
3 client code! It cannot be called, for example, from server code. If you are running a unit
4 test, check that your test case extends GWTTestCase and that GWT.create() is not called
5 from within an initializer or constructor.
6 at com.google.gwt.core.client.GWT.create(GWT.java:85)
7 at com.google.gwt.user.client.ui.UIObject.(UIObject.java:140)
8 ... 23 more
The above error means that GWT classes are not to be used in a standard JVM; they only work once they are compiled into JavaScript, and executed in a browser.
上面错误的意思是,GWT类不是使用标准的JVM(java虚拟机)环境;他们只有工作在编译为JavaScript的环境并执行浏览.
1.2 Some existing solutions
1.2 一些存在的解决办法
1.2.1 GWTTestCase
GWT provides a class to perform unit tests called GWTTestCase, however it suffers from a number of limitations: I
GWTTestCase是GWT提供一个类完成单位测试.然而它遭受一个编号限制:I
* The management of native events is only implemented from GWT 1.6. GWTTestCase does not allow testing the View part in an efficient manner as per the previous versions.
* 只有从GWT 1.6开始管理本地事务.GWTTestCase 不允许测试先前的一些版本展示部分的能力,.
* Slowness. In fact, this class launches a masked HostedMode (the development environment for GWT), which means it needs a few seconds to initialize itself.
* 事实,这个类是一个伪装(GWT开发环境).它需要一些初化它自己的意思
* Locating files. The unit tests have to be compiled by GWT. They must therefore be referenced by the MyApp.gwt.xml file. This complicates the application launch and packaging quite a bit, especially with Maven.
* 找出文件,使单元测通过GWT编译.它们必需参考MyApp.gwt.xml文件.这是个复杂难懂的应用,实际上是打包成小块,特别是Maven(基于项目对象模型(POM)项目管理工具).
Moreover, using the GWTTestCase and GWTTestSuite greatly limits access to Java APIs: the unit tests must be compatible with the GWT
compiler (which takes care of compiling the Java UI code into JavaScript). This means that, for example, using Java reflection is not possible.
此外,使用GWTTestCase和GWTTestSuite极大的限制访问Java APIs:单元测试必须兼容GUT编译(JavaScript里做Java UI编译).意思是例如不能使用Java映射.
It is therefore not possible to use test libraries such as Unitils or Easymock.
因此不能使用像Unitils 或 Easymock这样的测试库.
The list of Java classes emulated in JavaScript is available here.
可以到<a herf="http://code.google.com/intl/fr/webtoolkit/doc/1.6/RefJreEmulation.html">这</a>查看列举了在JavaScript中仿真Java类.
GWT 2.0 brings an improvement to GWTTestCase in that the class no longer uses native libraries to run tests. HtmlUnit is used instead of the browser used by Hosted Mode. In GWT 2, GWTTestCase is platform independent. But some limitations of GWTTestCase are still there: test execution is slow and there is no possibility to use a standard test framework.
GWT 2.0 带来了一个改进,GWTTestCase不允许使用本地库运行测试.HtmlUnit 是用来替换浏览器东道主模式的.在GWT 2, GWTTestCase独立的平台.但GWTTestCase有些限制:测试执行慢和不可能使用标准的测试框架.
1.2.2 Using interfaces
1.2.2使用接口.
A solution to test a GWT application is to not use GWT objects while testing but instead replace all GWT objects with mock objects that will work in a standard JVM. This solution presents a major inconvenience however. Since the GWT objects are not interfaces but rather concrete classes, we will have to modify the code of our application so that it uses interfaces (you can find an example here). This solution impacts the design of our application.
1.3 The heart of the problem
1.3 核心问题
In order to move forward, we have investigated why Google blocked the execution of GWT classes in a standard JVM. The reason is simple: a good portion of the code for these classes uses JSNI code. JSNI code is presented in the following manner:
在命令移动转向.我们调查为什么GWT类在标装的JVM中Google实行妨碍.一个简单原因:一个好的部分就是这些类使用的是JSNI(JavaScript本地接口)编码.JSNI码是下面的方式出现:
1 public static native void alert(String msg) /*-{ $wnd.alert(msg); }-*/;
This is a native function. It means that during the execution, the JVM will try to execute a function that has the same name as a DLL (or a .so). When these classes are compiled into JavaScript, the method is replaced by the code located within the /* and */. It’s the reason we cannot execute within a standard JVM, using non-JavaScript.
这是一个本地函数,它的意思是执行期间,JVM尝试执行同名的DLL(或者一个.so,DLL是windows系统, .so 是linux系统.).当这些类在JavaScript里编译,这个方法被替换回"/*"和"*/"之间的代码(方法体以/*-{开头 以}-*/结尾,最后要加分号。).这就是不能在标准的JVM中扫行的原因.
In addition, part of the GWT behaviour is not implemented in Java, but in JavaScript (or by the brower’s HTML rendering system). Even if we succeed in circumventing the problem of the native methods, we have to find a solution to reimplement this behaviour.
附:部分GWT行为在Java里不生效的.但在JavaScript(或HTML浏览系统)中有效.甚至,如果我们成功规避本地方法的问题.我们找到一个解决办法
源文http://www.infoq.com/articles/gwt_unit_testing
Unit and Integration Testing for GWT Applications
关于GWT应用的单元测试与集成测试.
GWT is a framework developed by Google for building AJAX enabled web applications using the Java programming language. It comprises:
GWT是一个Google用来使用java程序语言 构建AJAX web应用的开发框架,包括:
1. An API for creating GUI (similar to Swing), which manipulates the web browser's Document Object Model (DOM).
1. 一个创建GUI(类似于Swing)的API,使用web浏览文档对象(DOM)
2. A Java-to-JavaScript compiler.
2.一个Java 到 JavaScript编译器.
3. An environment for running and debugging GWT applications.
3.一个运行和调试GWT 应用的环境.
This approach offers some advantages:
提供一些优点:
* Assuming you know Java, there is no need to learn a new programming language.
* 如果你懂得Java,这将不需要你学习新的编程语言.
* You can build an AJAX enabled web application without writing JavaScript or HTML.
* 你可以构建一个web应用使用AJAX不需要写JavaScript or HTML.
* Everything is written in Java, so Java's advanced development tools, including debugging and re-factoring support, are available.
* 一切都是通过Java,那Java高级开发工具,可得到的包括调试和重复工厂化支持.
* GWT shields the developer from browser idiosyncrasies
* GWT保护开发者浏览行为
Finally, since everything is in Java (even the View part of the MVC pattern), we should be able to create UI tests easily. This article explores some approaches.
最后,因为所有事都在Java(even the View part of the MVC pattern),我们能很容易的测试UI.本文近一步探讨.
1. First attempt
1.首先尝试
1.1 GWT using TDD
1.1 GWT采用TDD(测试驱动开发)
We will start with a simple example: we want to display a text entry field, a button and a label. When we click on the button, the text field content entered by the user is placed in the label. The corresponding test (in JUnit 4) will be:
我们装开始一个简单的例子:想显示一个文本框.一个按钮和一个标签.当用户点按钮时文本框内容放到标签中.相应的(JUnit 4)测试如下:
1 @Test
2 public void testClick() {
3 GwtExample view = new GwtExample();
4 Assert.assertNull(view.getLabel().getText());
5 view.getTextBox().setText("my text");
6 // creation of a basic "click" event
7 NativeEvent event = Document.get().createClickEvent(0, 0, 0, 0, 0, false, false, false, false); // dispatch de l'évènement DomEvent.fireNativeEvent(event, view.getButton());
8 Assert.assertEquals("my text", view.getLabel().getText());
9 }
Then, we can write the corresponding View code:
然后我们写相应的展示代码:
1 public class GwtExample extends Composite {
2 private Label label;
3 private TextBox textBox;
4 private Button button;
5
6 public GwtExample() {
7 FlowPanel fp = new FlowPanel();
8 textBox = new TextBox();
9 fp.add(textBox);
10 button = new Button("validate");
11 fp.add(button);
12 button.addClickHandler(new ClickHandler() {
13
14 public void onClick(ClickEvent event) {
15 label.setText(textBox.getText());
16
17 }
18 });
19 label = new Label("init");
20 fp.add(label);
21 initWidget(fp);
22 }
23
24 public Label getLabel() { return label; }
25 public TextBox getTextBox() { return textBox; }
26 public Button getButton() { return button; }
27 }
Finally, we launch the preceding JUnit test:
最后我们运行之前的单元测试:
1 java.lang.ExceptionInInitializerError ...
2 Caused by: java.lang.UnsupportedOperationException: ERROR: GWT.create() is only usable in
3 client code! It cannot be called, for example, from server code. If you are running a unit
4 test, check that your test case extends GWTTestCase and that GWT.create() is not called
5 from within an initializer or constructor.
6 at com.google.gwt.core.client.GWT.create(GWT.java:85)
7 at com.google.gwt.user.client.ui.UIObject.(UIObject.java:140)
8 ... 23 more
The above error means that GWT classes are not to be used in a standard JVM; they only work once they are compiled into JavaScript, and executed in a browser.
上面错误的意思是,GWT类不是使用标准的JVM(java虚拟机)环境;他们只有工作在编译为JavaScript的环境并执行浏览.
1.2 Some existing solutions
1.2 一些存在的解决办法
1.2.1 GWTTestCase
GWT provides a class to perform unit tests called GWTTestCase, however it suffers from a number of limitations: I
GWTTestCase是GWT提供一个类完成单位测试.然而它遭受一个编号限制:I
* The management of native events is only implemented from GWT 1.6. GWTTestCase does not allow testing the View part in an efficient manner as per the previous versions.
* 只有从GWT 1.6开始管理本地事务.GWTTestCase 不允许测试先前的一些版本展示部分的能力,.
* Slowness. In fact, this class launches a masked HostedMode (the development environment for GWT), which means it needs a few seconds to initialize itself.
* 事实,这个类是一个伪装(GWT开发环境).它需要一些初化它自己的意思
* Locating files. The unit tests have to be compiled by GWT. They must therefore be referenced by the MyApp.gwt.xml file. This complicates the application launch and packaging quite a bit, especially with Maven.
* 找出文件,使单元测通过GWT编译.它们必需参考MyApp.gwt.xml文件.这是个复杂难懂的应用,实际上是打包成小块,特别是Maven(基于项目对象模型(POM)项目管理工具).
Moreover, using the GWTTestCase and GWTTestSuite greatly limits access to Java APIs: the unit tests must be compatible with the GWT
compiler (which takes care of compiling the Java UI code into JavaScript). This means that, for example, using Java reflection is not possible.
此外,使用GWTTestCase和GWTTestSuite极大的限制访问Java APIs:单元测试必须兼容GUT编译(JavaScript里做Java UI编译).意思是例如不能使用Java映射.
It is therefore not possible to use test libraries such as Unitils or Easymock.
因此不能使用像Unitils 或 Easymock这样的测试库.
The list of Java classes emulated in JavaScript is available here.
可以到<a herf="http://code.google.com/intl/fr/webtoolkit/doc/1.6/RefJreEmulation.html">这</a>查看列举了在JavaScript中仿真Java类.
GWT 2.0 brings an improvement to GWTTestCase in that the class no longer uses native libraries to run tests. HtmlUnit is used instead of the browser used by Hosted Mode. In GWT 2, GWTTestCase is platform independent. But some limitations of GWTTestCase are still there: test execution is slow and there is no possibility to use a standard test framework.
GWT 2.0 带来了一个改进,GWTTestCase不允许使用本地库运行测试.HtmlUnit 是用来替换浏览器东道主模式的.在GWT 2, GWTTestCase独立的平台.但GWTTestCase有些限制:测试执行慢和不可能使用标准的测试框架.
1.2.2 Using interfaces
1.2.2使用接口.
A solution to test a GWT application is to not use GWT objects while testing but instead replace all GWT objects with mock objects that will work in a standard JVM. This solution presents a major inconvenience however. Since the GWT objects are not interfaces but rather concrete classes, we will have to modify the code of our application so that it uses interfaces (you can find an example here). This solution impacts the design of our application.
1.3 The heart of the problem
1.3 核心问题
In order to move forward, we have investigated why Google blocked the execution of GWT classes in a standard JVM. The reason is simple: a good portion of the code for these classes uses JSNI code. JSNI code is presented in the following manner:
在命令移动转向.我们调查为什么GWT类在标装的JVM中Google实行妨碍.一个简单原因:一个好的部分就是这些类使用的是JSNI(JavaScript本地接口)编码.JSNI码是下面的方式出现:
1 public static native void alert(String msg) /*-{ $wnd.alert(msg); }-*/;
This is a native function. It means that during the execution, the JVM will try to execute a function that has the same name as a DLL (or a .so). When these classes are compiled into JavaScript, the method is replaced by the code located within the /* and */. It’s the reason we cannot execute within a standard JVM, using non-JavaScript.
这是一个本地函数,它的意思是执行期间,JVM尝试执行同名的DLL(或者一个.so,DLL是windows系统, .so 是linux系统.).当这些类在JavaScript里编译,这个方法被替换回"/*"和"*/"之间的代码(方法体以/*-{开头 以}-*/结尾,最后要加分号。).这就是不能在标准的JVM中扫行的原因.
In addition, part of the GWT behaviour is not implemented in Java, but in JavaScript (or by the brower’s HTML rendering system). Even if we succeed in circumventing the problem of the native methods, we have to find a solution to reimplement this behaviour.
附:部分GWT行为在Java里不生效的.但在JavaScript(或HTML浏览系统)中有效.甚至,如果我们成功规避本地方法的问题.我们找到一个解决办法
作者: surpass_li 发布时间: 2010-06-23
这种中英混杂的输出好不友好。
作者: kafka0102 发布时间: 2010-06-23
回复 kafka0102
谢谢,初次翻译等译完后整理一版干净的:)
谢谢,初次翻译等译完后整理一版干净的:)
作者: surpass_li 发布时间: 2010-06-23
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28