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() {
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.