Why is my JSONObject related unit test failing?

The class JSONObject is part of the android SDK. That means that is not available for unit testing by default.

From http://tools.android.com/tech-docs/unit-testing-support

The android.jar file that is used to run unit tests does not contain any actual code - that is provided by the Android system image on real devices. Instead, all methods throw exceptions (by default). This is to make sure your unit tests only test your code and do not depend on any particular behaviour of the Android platform (that you have not explicitly mocked e.g. using Mockito).

When you set the test options to

testOptions {
    unitTests.returnDefaultValues = true
}

you are fixing the "Method ... not mocked." problem, but the outcome is that when your code uses new JSONObject() you are not using the real method, you are using a mock method that doesn't do anything, it just returns a default value. That's the reason the object is null.

You can find different ways of solving the problem in this question: Android methods are not mocked when using Mockito


As Lucas says, JSON is bundled up with the Android SDK, so you are working with a stub.

The current solution is to pull JSON from Maven Central like this:

dependencies {
    ...
    testImplementation 'org.json:json:20210307'
}

You can replace the version 20210307 with the the latest one depending on the Android API. It is not known which version of the maven artefact corresponds exactly/most closely to what ships with Android.

Alternatively, you can download and include the jar:

dependencies {
    ...
    testImplementation files('libs/json.jar')
}

Note that you also need to use Android Studio 1.1 or higher and at least build tools version 22.0.0 or above for this to work.

Related issue: #179461