powermock – real power for testing untestable stuff

Last weekend was spent to make my Chrome extension base on GWT working with content scripts written using GWT. As one of main concept is to keep my trust for my code on appropriate level. To achieve this I’m practicing in TDD. For testing purposes I use JUnit 4.x and for mocking Mockito.

I’d like to spend some thime on MockitoMockito is really powerful mocking framework with sweat features, all ones you can find here http://code.google.com/p/mockito/wiki/FeaturesAndMotivations. As for me I really like nice and laconic syntax ans simplicity.

The information above is not a main reason of this post, the main one is to tell about my experience to test code using GWT classes e.g. finals and statics. After some time spent to google something will be helpful I’v found out PowerMock. This framework provides abilities to mock static classes and final methods and as you might know GWT uses a lot of such stuff.

For example I need to test method which handles browser page’ events from content script and pass related data to background page to process those data. The issue was to assign event for whole page, this is impossible as RootPanel.get() does not support event handlers. So I’ve found out another way – use Event.addNativePreviewHandler and NativePreviewHandler to hande events.

So I created implementation of NativePreviewHandler and started from test. Logic is quite simple but I was unable to use GWT code in my POJT (Plain Old Java Test), as NativeEvent returned by NativePreviewEvent as an argument for method NativePreviewHandler.onPreviewNativeEvent requires GWT.create method call, which in turn need a GWT context to be set up. But this is not what I want.  I’d like to have simple and fast tests without necessity to compile Java to JavaScript and only after to test.

In this situation I need mock GWT classes to be able to test my code, but Mockito does not support mocking for static classes and final methods. So PowerMock solved my trouble. Example of usage:

@PrepareOnlyThisForTest({ NativeEvent.class, SelectionProvider.class })
@SuppressStaticInitializationFor({ "com.google.gwt.dom.client.NativeEvent" })
public class ContentScriptEventHandlerTest {
	private ChromePort port;
        // Creates mock for class with final methods
	private NativeEvent event = PowerMockito.mock(NativeEvent.class);
	public void setUp() throws Exception {
                // Creates mock for static class
                // assign return value for method from static class
                // there was stackoverflow and we do not need to call for equals
		PowerMockito.suppress(event.getClass().getMethod("equals", Object.class));
                // assign return values for final methods
	public void shouldNotConsumeNotMouseupEvent() throws Exception {
		ContentScriptEventHandler eventHandler = new ContentScriptEventHandler(port);
		NativePreviewEvent previewEvent = mock(NativePreviewEvent.class);
		verify(previewEvent, never()).consume();

That’s it. Have enjoy testing…

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.