相关疑难解决方法(0)

Android单元测试没有嘲笑

我按照了这个指南 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)

junit android unit-testing

44
推荐指数
3
解决办法
2万
查看次数

在 kotlin 中将 String 转换为 JsonObject 返回 null

编辑找到的解决方案:

在我的代码中发现了错误。我在单元测试中运行它,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)

java android json kotlin

8
推荐指数
1
解决办法
4万
查看次数

标签 统计

android ×2

java ×1

json ×1

junit ×1

kotlin ×1

unit-testing ×1