美文网首页
单元测试 -- IllegalAccessError: clas

单元测试 -- IllegalAccessError: clas

作者: None_Ling | 来源:发表于2021-09-26 19:26 被阅读0次

    背景

    在运行Android单元测试的过程中遇到了下面问题,提示Mock失败:

    org.objenesis.ObjenesisException: java.lang.reflect.InvocationTargetException
    
        at org.objenesis.instantiator.sun.SunReflectionFactoryHelper.newConstructorForSerialization(SunReflectionFactoryHelper.java:54)
        at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.<init>(SunReflectionFactoryInstantiator.java:41)
        at org.objenesis.strategy.StdInstantiatorStrategy.newInstantiatorOf(StdInstantiatorStrategy.java:67)
        at org.objenesis.ObjenesisBase.getInstantiatorOf(ObjenesisBase.java:94)
        at org.powermock.reflect.internal.WhiteboxImpl.newInstance(WhiteboxImpl.java:259)
        at org.powermock.reflect.Whitebox.newInstance(Whitebox.java:139)
        at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.getPowerMockTestListenersLoadedByASpecificClassLoader(AbstractTestSuiteChunkerImpl.java:95)
        at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:174)
        at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:48)
        at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createTestDelegators(AbstractTestSuiteChunkerImpl.java:108)
        at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.<init>(JUnit4TestSuiteChunkerImpl.java:71)
        at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.<init>(AbstractCommonPowerMockRunner.java:36)
        at org.powermock.modules.junit4.PowerMockRunner.<init>(PowerMockRunner.java:34)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    Caused by: java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.objenesis.instantiator.sun.SunReflectionFactoryHelper.newConstructorForSerialization(SunReflectionFactoryHelper.java:44)
        ... 26 more
    Caused by: java.lang.IllegalAccessError: class jdk.internal.reflect.ConstructorAccessorImpl loaded by org.powermock.core.classloader.MockClassLoader @429bd883 cannot access jdk/internal/reflect superclass jdk.internal.reflect.MagicAccessorImpl
        at java.base/java.lang.ClassLoader.defineClass1(Native Method)
        at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
        at org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(MockClassLoader.java:262)
        at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:206)
        at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass1(DeferSupportingClassLoader.java:89)
        at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:79)
        ... 31 more
    

    而从Stackoverflow上查找,说是通过PowerMockIgnore忽略掉reflect可以通过,但是Ignore后,同样无法运行

    @PowerMockIgnore("jdk.internal.reflect.*")
    

    MockClassLoader cannot access jdk/internal/reflect superclass jdk.internal.reflect.MagicAccessorImpl

    问题解决方案

    其实本质原因是因为JDK版本的问题,需要在Configuration中把JRE换成1.8即可


    image.png

    相关文章

      网友评论

          本文标题:单元测试 -- IllegalAccessError: clas

          本文链接:https://www.haomeiwen.com/subject/dltcnltx.html