On Feb 2, 10:19 pm, Kevin Q <[email protected]> wrote: > Our application consumes JSON web services. We're using JSONParser, > returning a JSONObject, and construct domain models from the result > JSONObject. I'd like to test that piece of code, but it seems I cannot > use JSONObject in JVM unit tests, as it depends on native code. > > java.lang.UnsatisfiedLinkError: > com.google.gwt.core.client.JavaScriptObject.createObject()Lcom/google/ > gwt/core/client/JavaScriptObject; > at com.google.gwt.core.client.JavaScriptObject.createObject(Native > Method) > at com.google.gwt.json.client.JSONObject.<init>(JSONObject.java:46) > [output cut...] > > I'm wondering is there a way to mock JSONObject, use a hash map to > hold the key/value pairs, which enables me to run these tests in JVM? > JSONObject is an interface, so EasyMock can't help here. (correct me > if I'm wrong) > > Also, I tried creating a class MockJSONObject extending JSONObject, > and replace the put/get logic with HashMap based implementation but > still no avail, because the JSONObject constructor calls native JS > code as well... > > Any input is appreciated.
I'd suggest giving up on using JSONObject altogether. I'm modeling my objects as interfaces and have them implemented by a JavaScriptObject, I parse a JSON string using JsonUtils.unsafeParse(). That way I can easily mock the interface in unit tests. I don't serialize objects to JSON, but if I were to do it, I'd probably use "new JSONObject(jso).toString()", which can easily be "hidden" (as well as the JsonUtils.unsafeParse) in a JSON class with parse/stringify methods. You could easily switch between using a pure- Java library or JsonUtils/JSONObject using <super-source/> (two versions of the JSON class, one in pure-Java that is used in DevMode and pure-Java tests, and one to be compiled to JS) -- You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
