我遵循了本指南 https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support 但我被这个错误困住了:
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.
我按照指南所说的那样通过 Gradle 构建进行了修改,但没有任何区别
testOptions {
unitTests.returnDefaultValues = true
}
最佳答案
JSON 与 Android SDK bundle 在一起,因此您只会遇到一个 stub 。你可以拉入一个 JSON jar,它会提供真实的对象来使用。
为此,您需要将其添加到您的 build.gradle:
testImplementation 'org.json:json:20140107'
或者,您可以下载并包含该 jar。
testCompile files('libs/json.jar')
请注意,最新版本的 JSON 是为 Java 8 构建的,因此您需要获取 20140107 您可能还需要清理和重建项目。
关于Android单元测试没有被 mock ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29402155/