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

查看:115
本文介绍了GWT嘲笑框架的综合优点/缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣为我的GWT应用程序使用正确的模拟框架。我的理解是Mockito,EasyMock和jMock是Java中最受欢迎的程序。有人可以列出他们最熟悉的模拟框架的优点/缺点,因为它与GWT有关,以帮助像我这样的GWT测试noobs?

预先感谢。 / p>

解决方案

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

http://www.gdevelop.com/w/blog/2010/01/10/testing-gwt-rpc-services/



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



可以为GWT创建单元测试客户端代码,如果GWT应用程序遵循模型视图演示者模式。假设我们正在测试所谓的演示者(逻辑),我们可以用任何模拟框架来嘲笑所谓的显示。以下是使用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);
给定(display.getShowResultButton())。willReturn(button);
given(display.getResultLabel())。willReturn(label);
ResultPresenter演示者=新的ResultPresenter();
presenter.bind(display);

//当
button.click();

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

}

以下是主讲人: p>

  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显示器显示;

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;

公共类MockButton实现HasClickHandlers {

私人HandlerManager handlerManager = new HandlerManager(this);

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

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

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

}

调用Presenter在'when'部分显示.showResult()而不是button.click(),但是您可以看到事件循环的嘲讽也是可能的。



Google GIN可能很因为它允许根据运行时/测试上下文来绑定不同的实例。在非GWTTestCase演示者测试中,GIN可以替换为Guice。



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


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.

解决方案

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.

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

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