Android unit test not mocked
Solution 1
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.
Solution 2
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
Solution 3
You need to add the following to the build.gradle:
android {
// ...
testOptions {
unitTests.returnDefaultValues = true
}
}
and
dependencies {
//...
testImplementation 'org.json:json:20180813'
}
Solution 4
Solution for the problem in if you're using Kotlin DSL:
testOptions {
unitTests.isReturnDefaultValues = true
}
testImplementation("org.json:json:20210307")
user1634451
Updated on October 18, 2021Comments
-
user1634451 over 2 years
I followed this guide https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support but i am stuck with this error:
junit.framework.AssertionFailedError: Exception in constructor: testSaveJson (java.lang.RuntimeException: Method put in org.json.JSONObject not mocked. See https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support for details.
I modified by Gradle build like the guide says but it doesn't make a difference
testOptions { unitTests.returnDefaultValues = true }
-
Donal Lafferty almost 9 yearsSeems you can now pull using Gradle, so
testCompile 'org.json:json:20140107'
is fine. -
IgorGanapolsky over 8 yearsWhy do you say that pulling from maven central doesn't work?
-
IgorGanapolsky over 8 yearsSo there is no way to use JSON in plain Junit tests?
-
Ben Pearson over 8 years@IgorGanapolsky it seems to be working again, so I'll update the answer
-
Nicolas Cornette about 8 yearsYou should use THIS version :
org.json:json:20080701
, which matches the Android version. In this version,getString()
returns null when value is json is null, instead of throwing an exception. -
Ben Pearson almost 8 years@NicolasCornette to which Android version are you referring?
-
Nicolas Cornette almost 8 years@BenPearson this is true on all android versions
-
tbm over 7 yearsSimply including this in build.gradle does not seem to import the correct libraries - I'm still getting the 'not mocked' error. What needs to be done to use the explicit library modules?
-
tbm over 7 yearsNever mind - turns out a clean/ rebuild fixed the problem.
-
alexandre-rousseau over 6 yearsCan we have more explanations about this?
-
Staszek over 4 yearsIt works, but as @tbm write - only after clean and rebuild. Can you add it to the answer?
-
Satish Ghuge about 4 yearsIs it required for Test Implementation only?
-
Satish Ghuge about 4 yearsLatest version available is 20190722
-
Nantoka over 2 yearsUsing Robolectric did the job for me. Interestingly the error message appeared after we upgraded to Android Studio Arctic Fox with AGP 7.0, with Android Studio 4.2 and AGP 4.2 we had no problem.