Comprehensive Pros / Cons of Mocking Framework for GWT

I am interested in using the correct falsification for my GWT application. I understand that Mockito, EasyMock and jMock are some of the most popular for Java. Can someone list the pros and cons for the mocking structure they are most familiar with, since it is associated with GWT to help GWT testing colleagues like me?

Thanks in advance.

+4
source share
2 answers

For server-side testing (RPC services), you can use any fake structure you want. The spring -test library may be useful for taunting HttpRequest, HttpSession, and other api servlet classes. However, you may have problems testing classes that extend RemoteServiceServlet, because they require a correctly encoded request. Here is an interesting project that solves this problem:

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

When it comes to testing client-side GWT code (the part that is compiled into Java Script), you can extend GWTTestCase. However, due to the limited emulation of the JRE library, in particular the lack of a reflection API, it would be impossible to use any mocking structure. Moreover, the GWTTestCase runtime is very slow, and for this reason is considered as the basis for integration testing, not unit testing.

You can create unit tests for GWT client code if the GWT application follows the model view template. Assuming we are testing the so-called "Presenter" (logical), we can mock the so-called "display" with any mockery. Here is an 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 a small class of helpers:

 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 be wise to call showenter.showResult () in the "when" section instead of button.click (), however, as you can see, a mockery of the circulation of events is also possible.

The Google GIN can be very useful as it allows you to link different instances depending on the runtime / test environment. In testing without a GWTTestCase, the GIN can be replaced with Guice.

Com.google.gwt.junit.GWTMockUtilities may also be useful.

+7
source

We are happy to use Gwt-test-utils for our GWT project.

Docking RPC calls with mockito is very simple:

First, you declare your mocked service in your test:

 @Mock private ServiceAsync service; 

then when you want to make fun of a successful callback:

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

Learn more about this: http://code.google.com/p/gwt-test-utils/wiki/MockingRpcServices

+2
source

All Articles