How to test multiple interfaces of a widget in MVP ?

48 views
Skip to first unread message

Leonel Gayard

unread,
Oct 21, 2011, 10:55:24 AM10/21/11
to google-we...@googlegroups.com
Hi,

I'm following the MVP pattern as described in the tutorials and in to Ray Ryan's talk in Google IO 2009.

I hit an issue when it comes to testing a widget that implements more than one interface.

Suppose you have a view for displaying, say, contact information; the view has a checkbox, indicating whether the user has a mobile phone and a text field, with his phone number.

When the checkbox is unchecked, the text field is disabled.

Now, according to Google's guidelines for MVP, I would create the following interface. Instead of Checkbox, the return type is HasValue<Boolean>; the nice thing about lighter-weight return types is that they can easily be mocked and run as fast JUnit tests in the JRE.

interface ContactView {
    HasValue<Boolean> getHasMobilePhone();

    ??? getMobilePhoneNumber();
}

Now, what should be the return type of method getMobilePhoneNumber() ? This return type should define both getValue() and setEnabled(boolean), and should be one that class TextField implements. HasValue<String> and HasEnabled are candidates, but each lacks the methods in the other, and I can only use one of them as a return type.

Have you faced the same problem ? How did you solve it ?

Thanks,
Leonel

Patrick Julien

unread,
Oct 21, 2011, 11:27:17 AM10/21/11
to google-we...@googlegroups.com
Make multiple methods, both returning the same value

interface ContactView {
    HasValue<Boolean> getHasMobilePhone();

    HasEnabled getMobilePhoneNumberEnabled();

    HasValue<String> getMobilePhoneNumber();
}

in the implementation, both methods return the same TextField

Thomas Broyer

unread,
Oct 21, 2011, 11:32:55 AM10/21/11
to google-we...@googlegroups.com
The solution is to move to the "part 2" article and follow the advice here: do not use HasXxxHandlers/HasValue/etc. interfaces but make up your own Presenter interface, single point of interaction from the view to the presenter (the view then pushes to the presenter, rather than the presenter pulling from the view).
Have a look at http://www.google.com/events/io/2010/sessions/gwt-continuous-build-testing.html too in how to organize your MVP (responsibilities of each part)

David

unread,
Oct 22, 2011, 12:10:22 PM10/22/11
to Google Web Toolkit
I prefer MVP part 2 as well. It lets me leverage @UiHandler in my uib
views thus further reducing boilerplate code in the presenter. I
found that GIN is essential to its success esp if you are considering
Junit testing without a web container. Others essentials are Mockito
and Jukito

Here's an example of how I've followed mvp part 2:

SampleUIB .java...
public class SampleUIB extends Composite implements SampleView

SampleView.java..
public interface SampleView{
public interface Presenter {
void buttonPressed(String fieldValue);
}
void loadData....);
}

SamplePresenter.java..
public class SamplePresenter implements SampleView.Presenter {..
@Inject private SampleRpcAsync sampleService;
@Inject private private Table3View view;

-david





The solution is to move to the "part 2" article and follow the advice
here:
> do not use HasXxxHandlers/HasValue/etc. interfaces but make up your own
> Presenter interface, single point of interaction from the view to the
> presenter (the view then pushes to the presenter, rather than the presenter
> pulling from the view).
> Have a look athttp://www.google.com/events/io/2010/sessions/gwt-continuous-build-te...too
Reply all
Reply to author
Forward
0 new messages