Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android单元测试未被模拟_Android_Unit Testing_Junit - Fatal编程技术网

Android单元测试未被模拟

Android单元测试未被模拟,android,unit-testing,junit,Android,Unit Testing,Junit,我遵循这个指南 但我被这个错误所困扰: 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-suppor

我遵循这个指南 但我被这个错误所困扰:

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 build,但这没有什么区别

 testOptions { 
    unitTests.returnDefaultValues = true
  }

我认为您正在尝试使用纯jUnit上的
Android框架
的一部分
org.json.JSONObject
运行测试

从文档:

用于运行单元测试的android.jar文件不包含任何实际代码,这些代码由真实设备上的android系统映像提供。相反,所有方法都会抛出异常(默认情况下)

我们知道,当使用诸如Log或TextUtils之类的类时,默认行为是有问题的,并将在将来的版本中评估可能的解决方案


您需要模拟可用于此目的的Android环境,或者InstrumentationTests

JSON与Android SDK捆绑在一起,因此您只需点击一个存根即可。您可以拉入一个JSON jar,它将提供真正的对象供您使用

要执行此操作,您需要将其添加到build.gradle中:

android {
  // ...
  testOptions { 
    unitTests.returnDefaultValues = true
  }
}
testImplementation'org.json:json:20140107'

或者,您可以下载并包含jar

testCompile files('libs/json.jar')
请注意,JSON的最新版本是为Java8构建的,因此您需要 您可能还需要清理和重建项目

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

}

您需要将以下内容添加到build.gradle中:

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


我们需要更多的信息。添加一些代码,解释您试图实现的目标。单元测试JSON(反)序列化程序?现在似乎可以使用Gradle拉取,因此
testCompile'org.JSON:JSON:20140107'
很好。您应该使用以下版本:
org.JSON:JSON:20080701
,它与Android版本匹配。在这个版本中,
getString()
在值为json为null时返回null,而不是抛出异常。@NicolasCornette您指的是哪个Android版本?@BenPearson所有Android版本都是如此,只是在构建中包含了这个。gradle似乎没有导入正确的库-我仍然收到“not mocked”错误。使用显式库模块需要做些什么?因此在普通Junit测试中没有办法使用JSON?我们可以对此进行更多解释吗?