【问题标题】:jsonarray from string returning as null来自字符串的jsonarray返回为null
【发布时间】:2021-03-24 16:20:42
【问题描述】:

将字符串转换为 JSONArray 的单行 sn-p 返回为 null,有什么想法吗?

JSONArray("""[{"id":1,"first":"John","last":"Doe"}]""")

这是我的依赖项

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.4'

    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
    implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
    androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"

    testImplementation 'junit:junit:4.13'
    testImplementation 'org.mockito:mockito-inline:2.13.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    implementation 'androidx.test.espresso:espresso-idling-resource:3.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.2.0'
    androidTestImplementation 'androidx.test:rules:1.3.0-alpha05'
    implementation 'com.google.android.material:material:1.1.0'
}

后续问题。使用 JUnit 从 SUT 调用 localhost。

【问题讨论】:

    标签: arrays json kotlin


    【解决方案1】:

    我假设您必须运行 Android 项目中模块/测试下存在的 unitTests。

    如果您尝试在 JUnit 测试中使用或访问 Android 相关代码,那么除非您没有在模块的 build.gradle 文件中添加以下代码行,否则它不会正常工作。

    testOptions {
        unitTests.returnDefaultValues = true
        unitTests.includeAndroidResources = true
    }
    

    一旦你添加了上述配置,任何对 Android 方法的访问都应该返回它们的默认值,无论你传递给它们什么。

    任何与android测试相关的东西都需要写在androidTest

    更多信息。

    http://tools.android.com/tech-docs/unit-testing-support#TOC-Method-...-not-mocked.-

    【讨论】:

    • 你是对的,该类是一个 SUT 并且正在测试中运行,但是,我仍然为空,仍在检查我是否一切都正确,有什么想法吗?
    • 请检查我的依赖关系
    • 经过我在testImplementation 'org.json:json:20140107' 中添加的一些研究后,还有另一个问题,我正在通过http://10.0.2.2:8080 在本地主机上进行测试(见第二个屏幕截图),我也很感谢您在那里的输入.谢谢
    • 您的项目似乎无法获取依赖项。暂时手动下载它,然后尝试测试一次,如果这有助于您解决问题。请手动下载并将其用作依赖项,请参阅stackoverflow.com/questions/29402155/…
    • 我在 2 天后重新访问了该项目,按照建议的链接添加了 jar,它有效,我评论了本地 jar 路径以强制它失败,它仍然有效,从 libs 中删除了 jar 文件及其继续工作,不确定发生了什么。
    猜你喜欢
    • 1970-01-01
    • 2012-10-09
    • 2023-03-07
    • 2015-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多