Thursday, July 29, 2010

How to unit test an event, based on the event bus?

Hello there,

I watched the presentation by Ray Ryan and have implemented a basic
framework based on the MVP pattern article [1]. However, what I don't
understand is how am I supposed to write my unit tests?

Let's say I have an architecture like this:

[client]
+--------- [events]
+-----------+--------- OKButtonEvent
+-------- [handlers]
+-----------+---------- OKButtonEventHandler
+--------- [presenters]
+-----------+---------- MyBooksPresenter
+--------- [views]
+-----------+---------- MyBooksView
+---------------------- [impl]
+-------------------------+------- MyBooksViewImpl
+--------- cfg
+-----------+---------- BookGinjector
+-----------+---------- BookGinModule
+--------- BookAppEntryPoint

I hope it's easy to figure out what is what. The difference from the
original pattern here is that, instead of using an internal interface
Display in the presenter, I'm using a MyBooksView interface (same
thing), which is then implemented by a MyBooksViewImpl class (let's
say it's a Composite).

The MyBooksView has a getOKButton() method, I attach a ClickHandler to
it in the presenter, when the user clicks the button, I fire an event
on the eventBus:

eventBus.fireEvent(new OKButtonEvent());

Eventually this is handled by the OKButtonEventHandler.

Oh yeah, and I use Gin for dependency injection, though that was
probably obvious from the above graph.

So what I would want to do now is to replace the MyBooksViewImpl class
with a MockMyBooksView. This wouldn't be a GWT Composite class,
instead it would be just a plain Java class that, let's say, prints
out OK on the console when the ok button is pressed.

So how to write a JUnit test that would simulate a mouse click?

A more complex example, let's say that the ok button adds a new Book
item to the list, and the presenter has a getBooks() method, so that I
can check if the book has actually been added. How to test this?
The problem with this is that, when I fire an event, it works
asynchronously after that... so how can I test if the item has been
added, when I don't know when will that happen?

I was trying to use Easymock but didn't get very far.


So all in all, can someone provide me with an example JUnit class how
to test the event bus?

Thanks, Csaba


1 - http://code.google.com/webtoolkit/articles/mvp-architecture.html

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment