简体   繁体   中英

Can't run tests with Robolectric 4.0.1, 'packageDebugUnitTestForUnitTest' fails

My robolectric tests aren't working after upgrading to Robolectric 4.0.1.

In my gradle configuration I am using testOptions.unitTests.includeAndroidResources = true and in 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.

  • 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. If I remove the testOptions.unitTests.includeAndroidResources = true option from the gradle config file then I am getting the following error:

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

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

Why did the tests work with version 3.6.1 but now they don't?

Remove android.enableUnitTestBinaryResources=true from gradle.properties. It helps in my case.

Please refer all the comments on this issue here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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