views:

159

answers:

1

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.

+3  A: 

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.

morisil