Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

android - UnitTest JSONObject shows null

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
        } 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...