JSON in JUnit test setup

1,281 views
Skip to first unread message

Brice

unread,
Oct 16, 2006, 5:57:30 AM10/16/06
to Google Web Toolkit
Hello,

I'm having problems setting up a JUnit test using JSON objects.

Here's my test case :

----------
package cern.ppt.ram.gwt.client;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.junit.client.GWTTestCase;

public class TestJsonSetup extends GWTTestCase {
protected void setUp() throws Exception {
// Moving this line to testJsonObjectCreation : the test
will work fine
// Here, it produces an exception
JSONObject json = new JSONObject();
}
public void testJsonObjectCreation() {
}
(...)
}
-----------

It produces the following stack trace - of course, if I move the
offending line to a test method, it works fine.

java.lang.UnsatisfiedLinkError: createBlankObject
at com.google.gwt.json.client.JSONObject.createBlankObject(Native
Method)
at com.google.gwt.json.client.JSONObject.<init>(JSONObject.java:157)
(...)

What am I missing ? is it possible to set up test JSON objects in the
setUp() method at all ?

Thanks,

Brice

Kelly Norton

unread,
Oct 16, 2006, 11:52:22 AM10/16/06
to Google-We...@googlegroups.com
Hi Brice,

There are currently some limitations in the way setUp and tearDown work in GWTTestCase. The way we interact with jUnit under the hood makes it impossible to instantiate any client classes during setUp and tearDown.

/kel

Geoffrey Drossart

unread,
Oct 17, 2006, 9:19:18 AM10/17/06
to Google Web Toolkit
Hello,

I do not use JUnit but I have exactly the same problem with this object
method:

public JSONObject jsonGet(){

JSONObject jsonObject = new JSONObject(); // problem here
jsonObject.put("iUserId", new JSONNumber(this.iUserId));
...
return jsonObject;
}

I can not create jsonObject...

Thanks for all,
Geoffrey

> ------=_Part_63996_20390647.1161013942161
> Content-Type: text/html; charset=ISO-8859-1
> X-Google-AttachSize: 2235
>
> Hi Brice,<br><br>There are currently some limitations in the way setUp and tearDown work in GWTTestCase. The way we interact with jUnit under the hood makes it impossible to instantiate any client classes during setUp and tearDown.
> <br><br>/kel<br><br><div><span class="gmail_quote">On 10/16/06, <b class="gmail_sendername">Brice</b> &lt;<a href="mailto:brice...@gmail.com">brice...@gmail.com</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
> <br>Hello,<br><br>I'm having problems setting up a JUnit test using JSON objects.<br><br>Here's my test case :<br><br>----------<br>package cern.ppt.ram.gwt.client;<br>import com.google.gwt.json.client.JSONObject;<br>import
> com.google.gwt.junit.client.GWTTestCase;<br><br>public class TestJsonSetup extends GWTTestCase {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;protected void setUp() throws Exception {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Moving this line to testJsonObjectCreation : the test
> <br>will work fine<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Here, it produces an exception<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JSONObject json = new JSONObject();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void testJsonObjectCreation() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;(...)<br>}<br>
> -----------<br><br>It produces the following stack trace - of course, if I move the<br>offending line to a test method, it works fine.<br><br>java.lang.UnsatisfiedLinkError: createBlankObject<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at com.google.gwt.json.client.JSONObject.createBlankObject
> (Native<br>Method)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at com.google.gwt.json.client.JSONObject.&lt;init&gt;(JSONObject.java:157)<br>(...)<br><br>What am I missing ? is it possible to set up test JSON objects in the<br>setUp() method at all ?<br>
> <br>Thanks,<br><br>Brice<br><br><br></blockquote></div><br>
>
> ------=_Part_63996_20390647.1161013942161--

Kelly Norton

unread,
Oct 17, 2006, 9:54:13 AM10/17/06
to Google-We...@googlegroups.com
Geoffrey,

I'm not sure I can be of much help without more context. However, based on what you are experiencing, I would suggest that you take a look at the Getting Started Guide ( http://code.google.com/webtoolkit/gettingstarted.html). You can use the steps there to ensure that you have everything setup properly.

If that doesn't help, let me know.

/kel

Geoffrey Drossart

unread,
Oct 17, 2006, 10:36:15 AM10/17/06
to Google Web Toolkit
I want to create a simple JSONObject to put in data of UserIDcard, an
java object...
--------------------------
<inherits name='com.google.gwt.json.JSON'/> has been added to my XML
module.
-------------------------
package server;

import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
...
public class UserIDcard {

private Integer iUserId;
...
public JSONObject jsonGet(){

JSONObject jsonObject= new JSONObject(); // problem
here (line 84)


jsonObject.put("iUserId", new JSONNumber(this.iUserId));
...
return jsonObject;
}
}

-------------------
When process try "JSONObject jsonObject= new JSONObject();", I receive
this error message

[WARN] StandardContext[]Exception while dispatching incoming RPC call
java.lang.UnsatisfiedLinkError: createBlankObject


at com.google.gwt.json.client.JSONObject.<init>(JSONObject.java:157)

at server.UserIDcard.jsonGet(UserIDcard.java:84)
.....
------------------

I hope it is more descriptive,
Thanks for your patience,
Geoffrey

Kelly Norton

unread,
Oct 17, 2006, 1:37:56 PM10/17/06
to Google-We...@googlegroups.com
It looks like you are using json objects on the server. The json library that comes with GWT only works on the client side. In fact, you should generally avoid using anything from a "client" package in your server code.

In order to create and parse json on the server, you can pick up one of several such libraries from http://json.org/. One of those will definitely give you the functionality you need on the server side, probably with very little change to the code you've already written.

Hope that helps.
/kel

On 10/17/06, Geoffrey Drossart <gdro...@gmail.com> wrote:

Geoffrey Drossart

unread,
Oct 18, 2006, 3:06:09 AM10/18/06
to Google Web Toolkit
OK, I understand now.

Thanks for help,
Geoffrey

Reply all
Reply to author
Forward
0 new messages