Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kover breaks Robolectric #89

Closed
cristan opened this issue Dec 7, 2021 · 7 comments · Fixed by #95
Closed

Kover breaks Robolectric #89

cristan opened this issue Dec 7, 2021 · 7 comments · Fixed by #95
Labels
Bug Bug issue type Kover Coverage Tool S: waiting for clarification Status: additional information required to proceed

Comments

@cristan
Copy link

cristan commented Dec 7, 2021

Tested on Kover 0.4.4:

plugins {
    id 'org.jetbrains.kotlinx.kover' version '0.4.4'
}

Dependencies:

testImplementation "org.robolectric:robolectric:4.7.3"

Test

@RunWith(RobolectricTestRunner::class)
class RobolectricTest {
    @Test
    fun emptyTest() {
        "".toString()
    }
}

When I run this, I get this exception:

java.lang.IllegalAccessException: class org.robolectric.res.ResourceTableFactory cannot access a member of class android.R$styleable with modifiers "private static transient"
java.lang.RuntimeException: java.lang.IllegalAccessException: class org.robolectric.res.ResourceTableFactory cannot access a member of class android.R$styleable with modifiers "private static transient"
	at org.robolectric.res.ResourceTableFactory.addMissingStyleableAttributes(ResourceTableFactory.java:100)
	at org.robolectric.res.ResourceTableFactory.lambda$newFrameworkResourceTable$0(ResourceTableFactory.java:20)
	at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:53)
	at org.robolectric.res.ResourceTableFactory.newFrameworkResourceTable(ResourceTableFactory.java:13)
	at org.robolectric.android.internal.AndroidTestEnvironment.getSystemResourceTable(AndroidTestEnvironment.java:394)
	at org.robolectric.android.internal.AndroidTestEnvironment.injectResourceStuffForLegacy(AndroidTestEnvironment.java:419)
	at org.robolectric.android.internal.AndroidTestEnvironment.loadAppPackage_measured(AndroidTestEnvironment.java:365)
	at org.robolectric.android.internal.AndroidTestEnvironment.lambda$loadAppPackage$3(AndroidTestEnvironment.java:358)
	at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:53)
	at org.robolectric.android.internal.AndroidTestEnvironment.loadAppPackage(AndroidTestEnvironment.java:358)
	at org.robolectric.android.internal.AndroidTestEnvironment.installAndCreateApplication(AndroidTestEnvironment.java:257)
	at org.robolectric.android.internal.AndroidTestEnvironment.lambda$createApplicationSupplier$0(AndroidTestEnvironment.java:229)
	at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:53)
	at org.robolectric.android.internal.AndroidTestEnvironment.lambda$createApplicationSupplier$1(AndroidTestEnvironment.java:226)
	at com.google.common.base.Suppliers$NonSerializableMemoizingSupplier.get(Suppliers.java:167)
	at org.robolectric.RuntimeEnvironment.getApplication(RuntimeEnvironment.java:71)
	at org.robolectric.android.internal.AndroidTestEnvironment.setUpApplicationState(AndroidTestEnvironment.java:194)
	at org.robolectric.RobolectricTestRunner.beforeTest(RobolectricTestRunner.java:325)
	at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:265)
	at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:88)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.IllegalAccessException: class org.robolectric.res.ResourceTableFactory cannot access a member of class android.R$styleable with modifiers "private static transient"
	at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:361)
	at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:591)
	at java.base/java.lang.reflect.Field.checkAccess(Field.java:1075)
	at java.base/java.lang.reflect.Field.get(Field.java:416)
	at org.robolectric.res.ResourceTableFactory.addMissingStyleableAttributes(ResourceTableFactory.java:98)

When I run it without the kover plugin, it succeeds just fine.

@a-ware
Copy link

a-ware commented Dec 7, 2021

I ran into a similar issue with an Android project where Kover broke tests using RobolectricTestRunner, but tests passed using the JUnit4 test runner instead.

The project used Kover v0.4.4 and Robolectric 4.3. I verified it also repros on Robolectric 4.7.3 or using the androidx.test.ext.junit.runners.AndroidJUnit4 test runner. Tests failed with this exception:

java.lang.VerifyError: Bad return type
Exception Details:
  Location:
    android/content/res/ResourcesImpl.$$robo$$android_content_res_ResourcesImpl$loadComplexColorForCookie(Landroid/content/res/Resources;Landroid/util/TypedValue;ILandroid/content/res/Resources$Theme;)Landroid/content/res/ComplexColor; @817: areturn
  Reason:
    Type 'java/lang/Object' (current frame, stack[0]) is not assignable to 'android/content/res/ComplexColor' (from method signature)
  Current Frame:
    bci: @817
    flags: { }
    locals: { 'android/content/res/ResourcesImpl', 'android/content/res/Resources', 'android/util/TypedValue', integer, 'android/content/res/Resources$Theme', '[I', 'java/lang/String', 'java/lang/Object', 'android/content/res/XmlResourceParser', 'android/util/AttributeSet', integer, 'java/lang/String' }
    stack: { 'java/lang/Object' }
  Bytecode:
    0x0000000: b200 8bc7 000b 128d b800 93b3 008b b200
    0x0000010: 8b3a 0519 0511 0264 5c2e 0460 4f2c b401
    0x0000020: b1c7 0006 a700 1019 0511 0265 5c2e 0460
    0x0000030: 4fa7 0036 1905 1102 665c 2e04 604f 1905
    0x0000040: 1102 675c 2e04 604f bb04 4359 bb01 2359
    0x0000050: b701 2413 0445 b601 2a2c b402 b9b6 0187
    0x0000060: b601 34b7 0446 bf19 0511 0268 5c2e 0460
    0x0000070: 4f2c b401 b1b9 01b2 0100 3a06 1905 1102
    0x0000080: 695c 2e04 604f 013a 0719 0511 026a 5c2e
    0x0000090: 0460 4f14 01d6 1906 b801 df19 0511 026b
    0x00000a0: 5c2e 0460 4f19 0613 0393 b603 9799 0006
    0x00000b0: a700 1019 0511 026c 5c2e 0460 4fa7 0204
    0x00000c0: 1905 1102 6d5c 2e04 604f 1905 1102 6e5c
    0x00000d0: 2e04 604f 2a19 061d 2cb4 01af 1304 48b6
    0x00000e0: 039b 3a08 1905 1102 6f5c 2e04 604f 1908
    0x00000f0: b804 4e3a 0919 0511 0270 5c2e 0460 4f19
    0x0000100: 08b9 0451 0100 5936 0a05 9f00 06a7 0010
    0x0000110: 1905 1102 715c 2e04 604f a700 3019 0511
    0x0000120: 0272 5c2e 0460 4f15 0a04 9f00 06a7 0010
    0x0000130: 1905 1102 735c 2e04 604f a700 1019 0511
    0x0000140: 0274 5c2e 0460 4fa7 ffae 1905 1102 755c
    0x0000150: 2e04 604f 150a 059f 0006 a700 1019 0511
    0x0000160: 0276 5c2e 0460 4fa7 0022 1905 1102 775c
    0x0000170: 2e04 604f 1905 1102 785c 2e04 604f bb03
    0x0000180: d759 1304 53b7 0454 bf19 0511 0279 5c2e
    0x0000190: 0460 4f19 08b9 0457 0100 3a0b 1905 1102
    0x00001a0: 7a5c 2e04 604f 190b 1304 59b6 019a 9900
    0x00001b0: 06a7 0010 1905 1102 7b5c 2e04 604f a700
    0x00001c0: 2619 0511 027c 5c2e 0460 4f19 0511 027d
    0x00001d0: 5c2e 0460 4f2b 1908 1909 1904 b804 5f3a
    0x00001e0: 07a7 0048 1905 1102 7e5c 2e04 604f 190b
    0x00001f0: 1304 61b6 019a 9900 06a7 0010 1905 1102
    0x0000200: 7f5c 2e04 604f a700 2319 0511 0280 5c2e
    0x0000210: 0460 4f19 0511 0281 5c2e 0460 4f2b 1908
    0x0000220: 1909 1904 b804 643a 0719 0511 0282 5c2e
    0x0000230: 0460 4f19 08b9 03a2 0100 1905 1102 835c
    0x0000240: 2e04 604f a700 d119 0511 0284 5c2e 0460
    0x0000250: 4f3a 0819 0511 0285 5c2e 0460 4f14 01d6
    0x0000260: b802 6919 0511 0286 5c2e 0460 4fbb 0016
    0x0000270: 59bb 0123 59b7 0124 1301 b8b6 012a 1906
    0x0000280: b601 2a13 0466 b601 2a1d 1905 1102 875c
    0x0000290: 2e04 604f b801 30b6 012a b601 34b7 0137
    0x00002a0: 3a09 1905 1102 885c 2e04 604f 1909 1908
    0x00002b0: b601 cc57 1905 1102 895c 2e04 604f 1909
    0x00002c0: bf19 0511 028a 5c2e 0460 4f14 01d6 b802
    0x00002d0: 6919 0511 028b 5c2e 0460 4fbb 0016 59bb
    0x00002e0: 0123 59b7 0124 1301 b8b6 012a 1906 b601
    0x00002f0: 2a13 01ba b601 2a1d 1905 1102 8c5c 2e04
    0x0000300: 604f b801 30b6 012a 1304 30b6 012a b601
    0x0000310: 34b7 0137 bf19 0511 028d 5c2e 0460 4f14
    0x0000320: 01d6 b802 6919 0511 028e 5c2e 0460 4f19
    0x0000330: 07b0                                   
  Exception Handler Table:
    bci [202, 570] => handler: 583
  Stackmap Table:
    same_frame(@14)
    append_frame(@39,Object[#281])
    same_frame(@52)
    same_frame(@103)
    append_frame(@179,Object[#207],Null)
    same_frame(@192)
    append_frame(@245,Object[#929],Object[#1131])
    append_frame(@272,Integer)
    same_frame(@285)
    same_frame(@304)
    same_frame(@317)
    same_frame(@330)
    same_frame(@349)
    same_frame(@362)
    same_frame(@393)
    append_frame(@436,Object[#207])
    same_frame(@449)
    same_frame(@484)
    same_frame(@508)
    same_frame(@521)
    full_frame(@553,{Object[#2],Object[#24],Object[#428],Integer,Object[#27],Object[#281],Object[#207],Object[#4],Object[#929],Object[#1131],Integer,Object[#207]},{})
    full_frame(@583,{Object[#2],Object[#24],Object[#428],Integer,Object[#27],Object[#281],Object[#207],Object[#4]},{Object[#335]})
    full_frame(@705,{Object[#2],Object[#24],Object[#428],Integer,Object[#27],Object[#281],Object[#207],Null},{})
    full_frame(@789,{Object[#2],Object[#24],Object[#428],Integer,Object[#27],Object[#281],Object[#207],Object[#4],Object[#929],Object[#1131],Integer,Object[#207]},{})

	at android.content.res.Resources.__constructor__(Resources.java:228)
	at android.content.res.Resources.<init>(Resources.java)
	at android.content.pm.PackageParser.parseBaseApk(PackageParser.java:1334)
	at android.content.pm.PackageParser.parseMonolithicPackage(PackageParser.java:1299)
	at android.content.pm.PackageParser.parsePackage(PackageParser.java:1022)
	at android.content.pm.PackageParser.parsePackage(PackageParser.java:1042)
	at org.robolectric.shadows.ShadowPackageParser.callParsePackage(ShadowPackageParser.java:47)
	at org.robolectric.android.internal.AndroidTestEnvironment.loadAppPackage_measured(AndroidTestEnvironment.java:314)
	at org.robolectric.android.internal.AndroidTestEnvironment.lambda$loadAppPackage$1(AndroidTestEnvironment.java:284)
	at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:53)
	at org.robolectric.android.internal.AndroidTestEnvironment.loadAppPackage(AndroidTestEnvironment.java:282)
	at org.robolectric.android.internal.AndroidTestEnvironment.installAndCreateApplication(AndroidTestEnvironment.java:178)
	at org.robolectric.android.internal.AndroidTestEnvironment.setUpApplicationState(AndroidTestEnvironment.java:169)
	at org.robolectric.RobolectricTestRunner.beforeTest(RobolectricTestRunner.java:301)
	at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:243)
	at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

@xouabita
Copy link

xouabita commented Dec 8, 2021

I ran into the same issue and I have added jvmArgs '-noverify' to the testOptions as mentioned here: https://stackoverflow.com/a/37593189

android {
  // ....

  testOptions {
    unitTests.all {
      jvmArgs '-noverify'
    }
  }
}

it fixed the issue for me :)

@shanshin
Copy link
Collaborator

shanshin commented Dec 9, 2021

Hi,
to eliminate the conflict between the Robolectric and the Kover, you need to add several classes to the instrumentation exclusions

    testOptions {
        unitTests.all {
            kover {
                excludes = ['android\\.R\\$styleable', 'com\\.android\\.internal\\.R\\$styleable']
            }
        }
    }

Adding a -noverify is redundant in this case.

@cristan
Copy link
Author

cristan commented Dec 14, 2021

It's a little weird that this is needed, but excluding these classes indeed works.

@mezpahlan
Copy link

mezpahlan commented Dec 21, 2021

If anyone is looking at this and using a Kotlin DSL build script this is what you need to write in the excludes configuration:

excludes = listOf("android\\.R\\\$styleable", "com\\.android\\.internal\\.R\\\$styleable")

Note the excessive escaping especially around the $ sign.

@mezpahlan
Copy link

Also note that for my project setup with Robolectric I had to use this exclusion:

excludes = listOf("android.*", "com\\.android.*")

@shanshin
Copy link
Collaborator

Fixed in 0.5.0-RC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Bug issue type Kover Coverage Tool S: waiting for clarification Status: additional information required to proceed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants