Android unit test not mocked

JSON is bundled up with the Android SDK, so you'll just be hitting a stub. You can pull in a JSON jar, which will provide real objects to use.

To do this, you'll need to add this to your build.gradle:

testImplementation 'org.json:json:20140107'

Alternatively, you can download and include the jar.

testCompile files('libs/json.jar')

Note that the latest version of JSON is built for Java 8, so you'll need to grab 20140107 You may also need to clean and rebuild the project.


I think you trying to run tests with org.json.JSONObject which is part of Android Framework on pure jUnit.

From docs:

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).

We are aware that the default behavior is problematic when using classes like Log or TextUtils and will evaluate possible solutions in future releases.

You need to emulate Android environment you can use for this purpose Robolectric or InstrumentationTests