I have got a problem connected with JSONObject.
@Test
public void toUrlTest() throws JSONException {
String url;
JSONObject json = new JSONObject();
json.put"id", 1);
json.put("email", "[email protected]");
url = JSONParser.toURLString(json);
assertEquals("id=1&[email protected]", url);
}
The problem is when I am debugging this test, it shows that nothing is put to json object.
json={org.json.JSONObject@826} "null"
I checked everything and I got no clue why this happen. JSONObject works fine in app. It only happens while testing.
PS. I have added in build.gradle this
testOptions {
unitTests.returnDefaultValues = true
}
TL;DR version of Yair Kukielka's response... (thanks!)
Add this to your build.gradle
file, as suggested by
testImplementation "org.json:json:20140107"
This will replace the stubbed Android library with one that works on the desktop.
Edit April '22: Comments suggest that this is the latest version:
testImplementation "org.json:json:20180813"
There are 2 types of unit tests in Android:
If your unit tests use classes provided by the Android SDK (like the JSONObject or a Context), you'll have to choose between:
OR
You have more useful info about this subject in this post.
By the way, in the post you'll learn a trick to convert your JSONObject unit test to a non-instrumented test that you can execute on your local JVM (without Roboelectric)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With