GWT 模拟框架的综合优缺点
我有兴趣为我的 GWT 应用程序使用正确的模拟框架。据我了解,Mockito、EasyMock 和 jMock 是 Java 中最流行的一些。有人可以列出他们最熟悉的与 GWT 相关的模拟框架的优点/缺点,以帮助像我这样的 GWT 测试菜鸟吗?
提前致谢。
I'm interested in using the right mocking framework for my GWT app. It's my understanding that Mockito, EasyMock, and jMock are some of the most popular for Java. Could someone list pros/cons for the mocking framework that they are most familiar with as it relates to GWT to help fellow GWT testing noobs like myself?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于服务器端测试(RPC 服务),您可以使用任何您想要的模拟框架。 spring-test 库对于模拟 HttpRequest、HttpSession 和 servlet api 的其他类可能很有用。不过,您在测试扩展 RemoteServiceServlet 的类时可能会遇到问题,因为它们需要正确编码的请求。这是解决这个问题的有趣项目:
http://www.gdevelop.com/w/blog/2010/01/10/testing-gwt-rpc-services/
当涉及到客户端GWT代码的测试时(编译成Java的部分脚本),您可以扩展 GWTTestCase。然而,由于JRE库的模拟有限,特别是缺乏反射API,不可能使用任何模拟框架。更重要的是,GWTTestCase 运行时非常慢,因此请考虑将其作为集成测试而不是单元测试的基础。
如果 GWT 应用程序遵循模型视图呈现器模式,则可以为 GWT 客户端代码创建单元测试。假设我们正在测试所谓的“Presenter”(逻辑),我们可以使用任何模拟框架来模拟所谓的“Display”。这是使用 Mockito 的示例单元测试:
这是演示者:
这是小帮助程序类:
在“when”部分调用 Presenter.showResult() 而不是 button.click() 是有意义的,但是正如您所看到的模拟事件循环也是可能的。
Google GIN 可能非常有用,因为它允许根据运行时/测试上下文绑定不同的实例。在非 GWTTestCase 演示者测试中,GIN 可以替换为 Guice。
com.google.gwt.junit.GWTMockUtilities 可能也非常有用。
For the server side testing (RPC services) you can use any mocking framework you wish. spring-test library might be useful for mocking HttpRequest, HttpSession, and other classes of servlet api. Still you might have problems with testing classes extending RemoteServiceServlet, as they require properly encoded request. Here is interesting project which solves this problem:
http://www.gdevelop.com/w/blog/2010/01/10/testing-gwt-rpc-services/
When it comes to testing of client side GWT code (the part which is compiled into Java Script), you can extend GWTTestCase. However due to limited emulation of JRE library, lack of reflection API in particular, it would be impossible to use any mocking framework. What is more, GWTTestCase runtime is very slow, and for this reason consider as a base for integration testing rather than unit testing.
It is possible to create unit tests for GWT client code if GWT application follows Model View Presenter pattern. Assuming we are testing so called "Presenter" (logic) we can mock so called "Display" with any mocking framework. Here is example unit test using Mockito:
Here is the presenter:
And here is small helper class:
It would make sense to call presenter.showResult() in 'when' section instead of button.click(), however as you can see mocking of event circulation is also possible.
Google GIN might be very useful, as it allows to bind different instances depending on runtime/test context. On non-GWTTestCase presenter test GIN can be replaced with Guice.
The com.google.gwt.junit.GWTMockUtilities might be also very useful.
我们很高兴在 GWT 项目中使用 Gwt-test-utils。
使用mockito 模拟 RPC 调用非常简单:
首先在测试中声明模拟的服务:
然后当您想要模拟成功的回调时:
更多信息:http://code.google.com/p/gwt-test-utils/wiki/MockingRpcServices
We're happily using Gwt-test-utils for our GWT project.
Mocking RPC calls with mockito is really easy :
First you declare your mocked service in your test :
then when you want to mock a successful callback :
More on that : http://code.google.com/p/gwt-test-utils/wiki/MockingRpcServices