android - 无法使用 Robolectric 4.0.1 运行测试, 'packageDebugUnitTestForUnitTest' 失败

标签 android kotlin robolectric

升级到 Robolectric 4.0.1 后,我的 robolectric 测试无法正常工作。

在我的 gradle 配置中,我使用 testOptions.unitTests.includeAndroidResources = true 和 gradle.properties android.enableUnitTestBinaryResources=true

这是一个用作示例的虚拟测试:

@RunWith(RobolectricTestRunner::class)
internal class MyRobolectricTests
{
    @Test
    fun my_test()
    {
        val someItems = ApplicationProvider.getApplicationContext().resources.getStringArray(R.array.array_of_strings)

        assertEquals(10, someItems.size)
    }
}

我遇到了 2 个不同的问题:

  1. 如果我运行测试,则会收到以下消息:

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ':packageDebugUnitTestForUnitTest'.

    Illegal character in opaque part at index 11: jar:file:C:\myproject\build\intermediates\apk_for_local_test\debugUnitTest\packageDebugUnitTestForUnitTest\apk-for-local-test.ap_

  1. 如果我从 gradle 配置文件中删除 testOptions.unitTests.includeAndroidResources = true 选项,则会出现以下错误:

android.content.res.Resources$NotFoundException: Resource ID #0x7f030007

在行 val someItems = ApplicationProvider.getApplicationContext()...

为什么测试适用于 3.6.1 版,但现在却不行?

最佳答案

从 gradle.properties 中删除 android.enableUnitTestBinaryResources=true。它对我的情况有帮助。

请引用有关此问题的所有评论here

关于android - 无法使用 Robolectric 4.0.1 运行测试, 'packageDebugUnitTestForUnitTest' 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53159805/

相关文章:

android - Kotlin - 类型不匹配 : required: Context found: Context?

android - 如何重置组合 View 动画的状态

java - 从 Robolectric 下的服务获取 WindowManager 时出现 NullPointerException

android - InjectView 在单元测试中不起作用

使用 robolectric 进行 Android gradle 测试

android - 使用任意 Boost 库进行奇怪的 NDK 编译

android - 为什么我的 Activity 是 logcat ="WIN DEATH Killing ProcessRecord"当我将 sd 卡安装到计算机时

android - MapView 非常慢, map 上有 1000 个覆盖项,即使只有 2 个可见项

Android - onStop() 将被延迟调用

java - Oracle 数组和 Hibernate