Android unit test not mocked

AndroidUnit TestingJunit

Android Problem Overview


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
  }

Android Solutions


Solution 1 - Android

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 - Android

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 - Android

You need to add the following to the build.gradle:

android {
  // ...
  testOptions { 
    unitTests.returnDefaultValues = true
  }
}

and

dependencies {
//...
    testImplementation 'org.json:json:20180813'
}

Solution 4 - Android

Solution for the problem in if you're using Kotlin DSL:

testOptions {
    unitTests.isReturnDefaultValues = true
}

testImplementation("org.json:json:20210307")

Solution 5 - Android

android {


testOptions {
    unitTests.returnDefaultValues = true
} }

dependencies {
testImplementation libs.leakCanaryNoOp
testImplementation tests.jUnit
testImplementation tests.mockito
testImplementation(tests.mokitoKotlin) {
    exclude group: "org.jetbrains.kotlin", module: "kotlin-stdlib"
    exclude group: "org.jetbrains.kotlin", module: "kotlin-runtime"
    exclude group: "org.jetbrains.kotlin", module: "kotlin-reflect"
    exclude group: "org.mockito", module: "mockito-core"
}
testImplementation tests.powerMock
testImplementation tests.powerMockApiMockito
testImplementation (tests.robolectric) {
    exclude group: 'org.robolectric', module: 'robolectric-resources:'
}
testImplementation (tests.robolectricShadowsSupport){
    exclude group: 'org.robolectric', module: 'robolectric'
}
kaptTest libs.daggerCompiler

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1634451View Question on Stackoverflow
Solution 1 - AndroidBen PearsonView Answer on Stackoverflow
Solution 2 - Androidar-gView Answer on Stackoverflow
Solution 3 - AndroidThiagoView Answer on Stackoverflow
Solution 4 - AndroidMohammadRezaView Answer on Stackoverflow
Solution 5 - AndroidSon Nguyen ThanhView Answer on Stackoverflow