简体   繁体   English

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

[英]Can't run tests with Robolectric 4.0.1, 'packageDebugUnitTestForUnitTest' fails

My robolectric tests aren't working after upgrading to Robolectric 4.0.1. 升级到Robolectric 4.0.1后,我的robolectric测试无法正常工作。

In my gradle configuration I am using testOptions.unitTests.includeAndroidResources = true and in gradle.properties android.enableUnitTestBinaryResources=true . 在我的gradle配置中,我使用testOptions.unitTests.includeAndroidResources = true并在gradle.properties中使用android.enableUnitTestBinaryResources=true

Here's a dummy test to use as en example: 这是一个虚拟测试,用作示例:

@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)
    }
}

I am experiencing 2 separate problems: 我遇到了两个不同的问题:

  1. If I run the test then I am getting the following message: 如果我运行测试,那么我收到以下消息:

FAILURE: Build failed with an exception. FAILURE:构建因异常而失败。

  • What went wrong: Execution failed for task ':packageDebugUnitTestForUnitTest'. 出了什么问题:任务执行失败':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_ 索引11的不透明部分中的非法字符:jar:file:C:\\ myproject \\ build \\ intermediates \\ apk_for_local_test \\ debugUnitTest \\ packageDebugUnitTestForUnitTest \\ apk-for-local-test.ap_

  1. If I remove the testOptions.unitTests.includeAndroidResources = true option from the gradle config file then I am getting the following error: 如果我从gradle配置文件中删除testOptions.unitTests.includeAndroidResources = true选项,那么我收到以下错误:

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

In the line val someItems = ApplicationProvider.getApplicationContext()... 在行val someItems = ApplicationProvider.getApplicationContext()...

Why did the tests work with version 3.6.1 but now they don't? 为什么测试适用于3.6.1版,但现在却没有?

Remove android.enableUnitTestBinaryResources=true from gradle.properties. 从gradle.properties中删除android.enableUnitTestBinaryResources=true It helps in my case. 这有助于我的情况。

Please refer all the comments on this issue here 请在此处参阅有关此问题的所有评论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM