GWT 模拟框架的综合优缺点 [英] Comprehensive Pros/Cons of Mocking Frameworks for GWT

查看:24
本文介绍了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?

提前致谢.

推荐答案

对于服务器端测试(RPC 服务),您可以使用任何您希望的模拟框架.spring-test 库对于模拟 HttpRequest、HttpSession 和其他 servlet api 类可能很有用.您仍然可能在测试扩展 RemoteServiceServlet 的类时遇到问题,因为它们需要正确编码的请求.这是解决这个问题的有趣项目:

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/

在测试客户端 GWT 代码(编译成 Java Script 的部分)时,您可以扩展 GWTTestCase.但是由于 JRE 库的模拟有限,特别是缺少反射 API,因此无法使用任何模拟框架.更重要的是,GWTTestCase 运行时非常慢,因此考虑作为集成测试而不是单元测试的基础.

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.

如果 GWT 应用程序遵循 Model View Presenter 模式,则可以为 GWT 客户端代码创建单元测试.假设我们正在测试所谓的演示者"(逻辑),我们可以使用任何模拟框架来模拟所谓的显示".这是使用 Mockito 进行单元测试的示例:

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:

import static org.mockito.BDDMockito.*;
import org.junit.Test;
import com.google.gwt.user.client.ui.HasText;

public class ResultPresenterTest {

    @Test
    public void shouldSetItWorksResultText() {
        // given
        ResultPresenter.Display display = mock(ResultPresenter.Display.class);
        MockButton button = new MockButton();
        HasText label = mock(HasText.class);
        given(display.getShowResultButton()).willReturn(button);
        given(display.getResultLabel()).willReturn(label);
        ResultPresenter presenter = new ResultPresenter();
        presenter.bind(display);

        // when
        button.click();

        // then
        verify(label).setText("It works");
    }

}

主持人:

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.HasText;

public class ResultPresenter {

    private Display display;

    public interface Display {
        HasClickHandlers getShowResultButton();
        HasText getResultLabel();
    }

    public void bind(final Display display) {
        this.display = display;
        display.getShowResultButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                showResult();
            }
        });
    }

    public void showResult() {
        display.getResultLabel().setText("It works");
    }

}

这里是小助手类:

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.event.shared.HandlerRegistration;

public class MockButton implements HasClickHandlers {

    private HandlerManager handlerManager = new HandlerManager(this);

    public void click() {
        handlerManager.fireEvent(new ClickEvent() {
        });
    }

    @Override
    public HandlerRegistration addClickHandler(ClickHandler handler) {
        return handlerManager.addHandler(ClickEvent.getType(), handler);
    }

    @Override
    public void fireEvent(GwtEvent<?> event) {
        handlerManager.fireEvent(event);
    }

}

在when"部分调用presenter.showResult()而不是button.click()是有意义的,但是正如你所看到的,模拟事件循环也是可能的.

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 可能非常有用,因为它允许根据运行时/测试上下文绑定不同的实例.在非 GWTTestCase 演示者测试 GIN 上可以用 Guice 替换.

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.

com.google.gwt.junit.GWTMockUtilities 也可能非常有用.

The com.google.gwt.junit.GWTMockUtilities might be also very useful.

这篇关于GWT 模拟框架的综合优缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆