13

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", "test@hotmail.com");
    url = JSONParser.toURLString(json);

    assertEquals("id=1&email=test@hotmail.com", 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
        } 
4
  • Where are you running the tests? On PC or on device/simulator?
    – hgoebl
    Nov 8, 2015 at 17:07
  • I use pc I guess, becouse there is no screen to choose device
    – wacik93
    Nov 8, 2015 at 17:32
  • Such tests don't work on PC with android.jar. android.jar contains only signatures of classes, not the implementation. Run the tests on device.
    – hgoebl
    Nov 8, 2015 at 18:28
  • Are there any additional settings in build.gradle required? Have you got some tips on how to do this? I am new in android
    – wacik93
    Nov 8, 2015 at 18:54

3 Answers 3

14

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"
2
  • nice and simple solution
    – dudi
    Nov 22, 2018 at 8:30
  • 1
    Thanks! I expected things in the "android" package to be stubbed but I did not expect org.json.JSONObject to be stubbed! Also, org.json:json:20180813 appears to be the latest version as of 2022.
    – gMale
    Apr 26, 2022 at 22:19
7

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)

1
  • Post was great to use json with local JVM just for testing purposes!
    – JCricket
    Apr 4, 2016 at 1:41
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 ..

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.