我按照了这个指南 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.
Run Code Online (Sandbox Code Playgroud)
我按照指南的说法修改了Gradle,但它并没有什么区别
testOptions {
unitTests.returnDefaultValues = true
}
Run Code Online (Sandbox Code Playgroud) 编辑找到的解决方案:
在我的代码中发现了错误。我在单元测试中运行它,Android 在单元测试中不使用 JSON 对象,因为它是 android 的一部分。这就是它返回 null 的原因。
问题:
我正在使用 JSONObject("string") 将我的 String 转换回 JsonObject
这是我的字符串的示例:
{
"sessions": [
{
"locations": [
{
"lat": "14.2294625",
"lng": "121.1509005",
"time": 1560262643000,
"speed": 0,
"speedLimit": 0
},
{
"lat": "14.2294576",
"lng": "121.1509498",
"time": 1560262713000,
"speed": 0,
"speedLimit": 0
},
{
"lat": "14.2294576",
"lng": "121.1509498",
"time": 1560262714000,
"speed": 0,
"speedLimit": 0
}
],
"name": "1.5645220491E12"
}
]
}
Run Code Online (Sandbox Code Playgroud)
它在我的 JsonObjects 上返回 null:
content = "the string above"
var obj = JSONObject(content.substring(content.indexOf("{"), content.lastIndexOf("}") + …Run Code Online (Sandbox Code Playgroud)