Testing MVP Applications when using Overlay Types

52 views
Skip to first unread message

Matt Raible

unread,
Sep 23, 2009, 3:05:07 PM9/23/09
to google-we...@googlegroups.com
Hello all,

I've recently converted my GWT application to use MVP, specifically using the gwt-presenter project. I'm also using Overlay Types and RequestBuilder to talk to JSON-based services on the backend.

Since Overlay Types use JSNI, it's not possible to do any JSON parsing in unit tests. The problem with not being able to do any JSON parsing is the callbacks will often call eventBus.fireEvent(GwtEvent) after the JSON parsing has happened. This means I can't fully test the flow of a presenter if event firing happens in a callback.

I wrote up a detailed explanation of this issue on my blog[1]. Does anyone here have any suggestions on how I might go about solving this problem?

Thanks,

Matt

[1] http://raibledesigns.com/rd/entry/building_gwt_applications_with_mvp

Matt Raible

unread,
Sep 23, 2009, 4:08:04 PM9/23/09
to eguardiola, Google-We...@googlegroups.com
Thanks - that helps somewhat. It seems to suggest that I need to create an interface for all my model objects. Great idea if I'm generating objects from server-side classes, otherwise cumbersome. Let's assume I'm willing to go through the effort of creating interfaces and two different implementations.

I'm using a JSOModel[1] class to convert JSON to objects. How would I go about converting my callback (see below) to eval JSON and populate objects?
service.getConversationContexts(new Callback<Representation>() {
public void onSuccess(Representation data) {
List<ConvCtxSummaryModel> models = new ArrayList<ConvCtxSummaryModel>();
JsArray<JSOModel> a = JSOModel.fromJson(data.getData()).getArray("definition");
for (int i = 0; i < a.length(); i++) {
ConvCtxSummaryModel model =
new ConvCtxSummaryModel(a.get(i).getObject(ConvCtxSummaryModel.ROOT));
models.add(model);
}
eventBus.fireEvent(new ConversationsLoadedEvent(models));
}
});
Thanks,

Matt

[1] http://raibledesigns.com/rd/entry/json_parsing_with_javascript_overlay

On Wed, Sep 23, 2009 at 1:36 PM, eguardiola <eduardo....@gmail.com> wrote:
Read the "Updates for GWT 2.0 (In-progress DRAFT)" section of:

http://code.google.com/p/google-web-toolkit/wiki/OverlayTypes

PJ Gray

unread,
Sep 23, 2009, 4:12:11 PM9/23/09
to google-we...@googlegroups.com
Couldn't you do something like make the class that actually does the parsing be a dependency of the callback (ie passed in).   Then create a mock version of that class that just returns whatever data (for verification)?   This mock version wouldn't be based off the JSNI overlay abstract class, while your 'real' one was.    Heck if you wanted, you could always implement the JSON parsing by hand for the mock class, without using the overlay.....it is, after all, just a way of making parsing easier, correct?   Then your testing could verify not only the flow of your app, but the JSON parsing as well.

-pj

PJ Gray

unread,
Sep 23, 2009, 4:38:22 PM9/23/09
to google-we...@googlegroups.com
Well, I am no expert....not by any means, more like a noob.   But couldn't you use JSONParser and the related classes from gwt.json.client to do the parsing?   Granted, I am basing this off of when I did JSON parsing with the awesome JSONRequest class out of the book 'Google Web Toolkit Applications'.   Using that class you actually get sent back a JavaScriptObject, which I just create a JSONObject from to start parsing.   However, assuming that you are just feeding 'fromJson' a string representing your JSON data, I am guessing that could be sent directly to JSONParser, no?

-pj

Thomas Broyer

unread,
Sep 23, 2009, 6:04:40 PM9/23/09
to Matt Raible, Google Web Toolkit

On 23 sep, 21:05, Matt Raible <m...@raibledesigns.com> wrote:
> Hello all,
>
> I've recently converted my GWT application to use MVP, specifically using
> the gwt-presenter project. I'm also using Overlay Types and RequestBuilder
> to talk to JSON-based services on the backend.
>
> Since Overlay Types use JSNI, it's not possible to do any JSON parsing in
> unit tests. The problem with not being able to do any JSON parsing is the
> callbacks will often call eventBus.fireEvent(GwtEvent) after the JSON
> parsing has happened. This means I can't fully test the flow of a presenter
> if event firing happens in a callback.
>
> I wrote up a detailed explanation of this issue on my blog[1]. Does anyone
> here have any suggestions on how I might go about solving this problem?
[...]
> [1]http://raibledesigns.com/rd/entry/building_gwt_applications_with_mvp

Your proposed solutions all look good to me:
- (except this one!) detect when unit tests are running: you actually
want to detect whether you're in "pure Java" or "GWT client", use
GWT.isClient()
- modify presenters and services: I'm experimenting a similar method:
I actually use a GWT-RPC Async interface but instead of having the
client stub generated (i.e. let GIN fallback to GWT.create()) I make
my own implementation using RequestBuilder and JSON parsing. You're
not forced to use GWT-RPC Async-like (using AsyncCalback) interfaces,
you can make your own callback interface if AsyncCallback doesn't fit
your needs; but all in all, what I mean is that the JSON parsing
should be hidden, an "implementation detail" of your service
implementation.
- make JSOModel an interface: that's part of my experiment too;
except that I'm envisionning changing from a wrapper (similar to your
BaseModel, but mine isn't generic and entirely implemented using JSNI
methods) to a JSO overlay when I'll switch to GWT 2.0. Just that,
because I actually never used GWT-RPC, I don't know how it works (or
not) when arguments and return values are interfaces, so using
AsyncCallback and GWT-RPC-like interfaces might not be a good choice
after all (I even code the "sync", RemoteService interface, to please
the Google Plug-in for Eclipse; I haven't used it yet, but I thought
about using dummy service servlets for some "demo apps")

Jeff Chimene

unread,
Sep 23, 2009, 8:44:09 PM9/23/09
to google-we...@googlegroups.com

Have you considered using gin/mock classes to test your overlays and
high-level RPC calls? I've found that technique to be quite helpful.

Reply all
Reply to author
Forward
0 new messages