我正在为一个需要 Android 特定组件可用的类编写测试。 使用的版本:
机器人电动:4.10.3 powermock:2.0.9
我收到此错误
Failed to instantiate DeepCloner. The DeepCloner implementation must have a one-arg constructor taking a Classloader as parameter.
java.lang.RuntimeException: Failed to instantiate DeepCloner. The DeepCloner implementation must have a one-arg constructor taking a Classloader as parameter.
at org.powermock.classloading.AbstractClassloaderExecutor.doCreateDeepCloner(AbstractClassloaderExecutor.java:108)
at org.powermock.classloading.AbstractClassloaderExecutor.createDeepCloner(AbstractClassloaderExecutor.java:97)
at org.powermock.classloading.AbstractClassloaderExecutor.executeWithClassLoader(AbstractClassloaderExecutor.java:52)
at org.powermock.classloading.SingleClassloaderExecutor.execute(SingleClassloaderExecutor.java:33)
at org.powermock.classloading.AbstractClassloaderExecutor.execute(AbstractClassloaderExecutor.java:40)
at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:75)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:589)
at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$2(SandboxTestRunner.java:290)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:99)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:840)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at org.powermock.classloading.AbstractClassloaderExecutor.doCreateDeepCloner(AbstractClassloaderExecutor.java:106)
... 13 more
Caused by: java.lang.ExceptionInInitializerError
at com.thoughtworks.xstream.converters.extended.FontConverter.<init>(FontConverter.java:56)
at com.thoughtworks.xstream.XStream.setupConverters(XStream.java:1008)
at com.thoughtworks.xstream.XStream.<init>(XStream.java:592)
at com.thoughtworks.xstream.XStream.<init>(XStream.java:514)
at com.thoughtworks.xstream.XStream.<init>(XStream.java:483)
at com.thoughtworks.xstream.XStream.<init>(XStream.java:429)
at com.thoughtworks.xstream.XStream.<init>(XStream.java:368)
at org.powermock.classloading.DeepCloner.<init>(DeepCloner.java:35)
... 19 more
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected java.lang.String java.text.AttributedCharacterIterator$Attribute.getName() accessible: module java.base does not "opens java.text" to unnamed module @fdc3812
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:199)
at java.base/java.lang.reflect.Method.setAccessible(Method.java:193)
at com.thoughtworks.xstream.converters.reflection.AbstractAttributedCharacterIteratorAttributeConverter.<clinit>(AbstractAttributedCharacterIteratorAttributeConverter.java:45)
... 27 more
我尝试在 build.gradle 中的 testOptions 下添加以下内容,但没有帮助
all {
jvmArgs '--add-opens=java.base/java.lang.reflect=ALL-UNNAMED',
'--add-opens=java.base/java.lang=ALL-UNNAMED',
'--add-opens=java.base/java.util=ALL-UNNAMED'
}
将此添加到我的 build.gradle 修复了 InaccessibleObjectException 的问题(允许对象变得可访问)
tasks.withType(Test) {
jvmArgs '--add-opens=java.base/java.lang.reflect=ALL-UNNAMED',
'--add-opens=java.base/java.lang=ALL-UNNAMED',
'--add-opens=java.base/java.util=ALL-UNNAMED',
'--add-opens=java.base/java.text=ALL-UNNAMED',
'--add-opens=java.desktop/java.awt.font=ALL-UNNAMED',
'--add-opens=java.base/java.security=ALL-UNNAMED'
}