views:

219

answers:

2

I have a class which uses JSNI to retrieve JSON data stored in the host page:

protected native JsArray<JsonModel> getModels() /*-{
    return $wnd.jsonData;
}-*/;

This method is called, and the data is then translated and process in a different method. How should I unit test this class, since I'm not able to instantiate (or seemingly mock?) JsArray?

What is the best way to unit test JSNI methods at all?

A: 

One obvious possibility is to extract the JSNI methods into an interface which is passed into the class via dependency injection. But if I only have one JSNI method (however key the method may be), it seems like this might be overkill - also, this doesn't solve the problem of actually testing the methods themselves... so, any better ideas?

Epaga
+1  A: 

The interface approach is the best approach, and not necessarily an overkill. As to the problem of actually testing the method - well, if it is just reading from $wnd.jsonData, there isn't any merit in testing the method. You are better off writing a server side test that confirms you have the right data in the host page.

If you really have some logic in a native method, you are better off writing a selenium/jsunit test case.

sri