'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
        } 


Solution 1:[1]

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"

Solution 2:[2]

There are 2 types of unit tests in Android:

  • Instrumented (slow and you need a device or emulator)
  • Non-instrumented or local (fast and you can execute them on the JVM on your computer)

If your unit tests use classes provided by the Android SDK (like the JSONObject or a Context), you'll have to choose between:

  • making your test an instrumented test

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)

Solution 3:[3]

You can use Mockito to mock JSONObject and return the JSON object you need.

Example:

@Test  
public void myTest(){

      JsonHttpResponseHandler handler = myClassUnderTest.getHandlerForGetCalendar(testFuture);
      JSONObject successResp = Mockito.mock(JSONObject.class);
      JSONArray events = Mockito.mock(JSONArray.class);
      JSONObject event = Mockito.mock(JSONObject.class);

  try{
    doReturn("Standup meeting with team..")
    .when(event).getString("Subject");

    doReturn("id_")
    .when(event).getString("Id");

    doReturn(1)
    .when(events).length();

    doReturn(event)
    .when(events).getJSONObject(0);

    doReturn(events)
    .when(successResp).getJSONArray("value");

    handler.onSuccess(200, null, successResp);

  }catch(Exception xx){
    JSONObject errorResp = null;
    try{
      errorResp = new JSONObject("{}"); // this will be null but it's fine for the error case..
    }catch(JSONException ex){
      throw new IllegalStateException("Test Exception during json error response construction. Cause: "+ex);
    }
    handler.onFailure(500, null, new IllegalStateException("Something went wrong in test helper handlerForGetCalendarOnSuccess. Cause: "+xx), errorResp);
  }

  // Assertions you need ..

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 PLNech
Solution 3 Gabe