How can I test class extends auto-generated Stub class from AIDL file

2
closed
z4hyoung
z4hyoung
Posted 3 months ago

How can I test class extends auto-generated Stub class from AIDL file #6513

Description

Hi,

I have a AIDL file like this,

// ITestBundle.aidl
package com.example.android;

interface ITestBundle {
    void test(in Bundle bundle);
}

and I'd like to test below implementation class,

class TestBundleImpl extends ITestBundle.Stub {
    @Override
    public void test(Bundle bundle) {
    }
}

But I got below error when running unit test

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.robolectric.util.ReflectionHelpers$7 (file:/Users/z4hyoung/.gradle/caches/modules-2/files-2.1/org.robolectric/shadowapi/3.8/d638f001e81ef737bb35db2964312360cc996a94/shadowapi-3.8.jar) to method java.lang.ClassLoader.getPackage(java.lang.String)
WARNING: Please consider reporting this to the maintainers of org.robolectric.util.ReflectionHelpers$7
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

java.lang.NoClassDefFoundError: android/content/pm/PackageManager$NameNotFoundException

	at java.base/java.lang.Class.getDeclaredConstructors0(Native Method)
	at java.base/java.lang.Class.privateGetDeclaredConstructors(Class.java:3137)
	at java.base/java.lang.Class.getConstructor0(Class.java:3342)
	at java.base/java.lang.Class.getConstructor(Class.java:2151)
	at org.robolectric.RobolectricTestRunner.getHooksInterface(RobolectricTestRunner.java:507)
	at org.robolectric.RobolectricTestRunner.beforeTest(RobolectricTestRunner.java:323)
	at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:245)
	at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:130)
	at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:42)
	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
	at org.robolectric.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:84)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Caused by: java.lang.ClassNotFoundException: couldn't load android.content.pm.PackageManager$NameNotFoundException
	at org.robolectric.internal.bytecode.SandboxClassLoader.maybeInstrumentClass(SandboxClassLoader.java:132)
	at org.robolectric.internal.bytecode.SandboxClassLoader.lambda$findClass$0(SandboxClassLoader.java:107)
	at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:50)
	at org.robolectric.internal.bytecode.SandboxClassLoader.findClass(SandboxClassLoader.java:106)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
	... 21 more
Caused by: java.lang.RuntimeException: failed to instrument android.content.pm.PackageManager$NameNotFoundException
	at org.robolectric.internal.bytecode.ClassInstrumentor.instrument(ClassInstrumentor.java:125)
	at org.robolectric.internal.bytecode.InvokeDynamicClassInstrumentor.instrument(InvokeDynamicClassInstrumentor.java:18)
	at org.robolectric.internal.bytecode.ClassInstrumentor.instrumentToBytes(ClassInstrumentor.java:67)
	at org.robolectric.internal.bytecode.SandboxClassLoader.lambda$maybeInstrumentClass$2(SandboxClassLoader.java:124)
	at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:50)
	at org.robolectric.internal.bytecode.SandboxClassLoader.maybeInstrumentClass(SandboxClassLoader.java:123)
	... 26 more
Caused by: java.lang.IllegalArgumentException
	at org.objectweb.asm.ClassReader.<init>(Unknown Source)
	at org.objectweb.asm.ClassReader.<init>(Unknown Source)
	at org.robolectric.internal.bytecode.ClassNodeProvider.createClassNode(ClassNodeProvider.java:24)
	at org.robolectric.internal.bytecode.ClassNodeProvider.getClassNode(ClassNodeProvider.java:16)
	at org.robolectric.internal.bytecode.ClassInstrumentor.isOverridingFinalMethod(ClassInstrumentor.java:218)
	at org.robolectric.internal.bytecode.ClassInstrumentor.createInstrumentableMethodIfNotAlreadyPresent(ClassInstrumentor.java:236)
	at org.robolectric.internal.bytecode.ClassInstrumentor.instrument(ClassInstrumentor.java:105)
	... 31 more


Process finished with exit code 255

Robolectric & Android Version

testImplementation 'org.robolectric:robolectric:3.8'

Link to a public git repo demonstrating the problem: https://github.com/z4hyoung/MyTest

Can anyone help me on this please? Thanks in advance!

realdadfish
realdadfish
Created 3 months ago

I don't know anything in this area, but the robolectric version seems to be quite old, could you please check if the issue persists with the most recent version, i.e. 4.5.1?

z4hyoung
z4hyoung
Created 3 months ago

It works with 4.5.1. Thank you! @realdadfish