GWT 模拟框架的综合优缺点

发布于 2024-09-13 13:22:21 字数 143 浏览 10 评论 0原文

我有兴趣为我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回眸一笑 2024-09-20 13:22:21

对于服务器端测试(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 的示例单元测试:

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() 是有意义的,但是正如您所看到的模拟事件循环也是可能的。

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:

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");
    }

}

Here is the presenter:

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");
    }

}

And here is small helper class:

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);
    }

}

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.

错々过的事 2024-09-20 13:22:21

我们很高兴在 GWT 项目中使用 Gwt-test-utils

使用mockito 模拟 RPC 调用非常简单:

首先在测试中声明模拟的服务:

@Mock
private ServiceAsync service;

然后当您想要模拟成功的回调时:

doSuccessCallback(result).when(service).myMethod(eq("argument"), any(AsyncCallback.class));

更多信息: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 :

@Mock
private ServiceAsync service;

then when you want to mock a successful callback :

doSuccessCallback(result).when(service).myMethod(eq("argument"), any(AsyncCallback.class));

More on that : http://code.google.com/p/gwt-test-utils/wiki/MockingRpcServices

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文