java.lang.IllegalAccessError: ... cannot access its superinterface .....

1,696 views
Skip to first unread message

logalvatron

unread,
Dec 21, 2008, 5:17:55 PM12/21/08
to PowerMock
Hey first off, great product. A Godsend, really.

Here's my problem. I'm trying to mock a class with a private
interface. Here's what the class looks like (logically)

public class ClassToMock {
....
private interface innerPrivateInterface
{
public void doOperation() throws Exception;
}

..........

}

Here's the stack trace:
java.lang.IllegalAccessError: class ClassToMock$4 cannot access its
superinterface
ClassToMock$innerPrivateInterface

at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.lang.ClassLoader.defineClass(ClassLoader.java:465)
at
org.powermock.core.classloader.MockClassLoader.loadMockClass
(MockClassLoader.java:201)
at
org.powermock.core.classloader.MockClassLoader.loadModifiedClass
(MockClassLoader.java:140)
at
org.powermock.core.classloader.DeferSupportingClassLoader.loadClass
(DeferSupportingClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2395)
at java.lang.Class.getDeclaredMethods(Class.java:1763)
at org.powermock.core.WhiteboxImpl.getAllMethods(WhiteboxImpl.java:
873)
at org.powermock.core.WhiteboxImpl.getMethods(WhiteboxImpl.java:1038)
at org.powermock.Whitebox.getMethods(Whitebox.java:412)
at org.powermock.PowerMock.mockStaticPartial(PowerMock.java:579)


I suspect this might be a limitation of the runtime.

Regards,

Leon Oosterwijk

Johan Haleby

unread,
Dec 22, 2008, 5:05:48 AM12/22/08
to powe...@googlegroups.com
Hi,

PowerMock doesn't have full support for inner classes yet. But since you requested the feature (well indirectly anyway) I've made it a bit easier for you. The reason why you got the IllegalAccessError was because the inner classes was not prepared for test using the @PrepareForTest annotation which may seem a bit strange. You could of course add the inner classes by specifying their fully-qualified names using the "fullyQualifedNames" attribute in the PrepareForTest annotation, e.g. @PrepareForTest(fullyQualifiedNames={"com.myapp.InnerInterface", "com.myapp.InnerClass"}). But even if you do that the interface would still be loaded by the wrong class-loader (because of a bug). So what I've done is to automatically prepare all inner member classes of class X if you prepare X for test which is probably what most people want anyway. If not you could always fallback to using the @PrepareOnlyThisForTest annotation. I've also added three new methods to the Whitebox class, these are Whitebox.getInnerClassType(..), Whitebox.getLocalClassType(..) and Whitebox.getAnonymousInnerClassType(..). These methods may help you with getting the class type for private inner, local or anonymous inner classes. So if you like you can check out the source from trunk using "svn checkout http://powermock.googlecode.com/svn/trunk/ powermock-read-only" and then do mvn install and give it a go. It would provide great feedback for us if you could do so and report back whether it works or not for you. You could check out http://code.google.com/p/powermock/source/browse/trunk/modules/module-test/powermock/junit4-test/src/test/java/samples/junit4/classwithinnermembers/ClassWithInnerMembersTest.java for an example.

Thanks for pointing this out,
Johan
Reply all
Reply to author
Forward
0 new messages