成功将PowerMock集成到我的项目中后,我遇到了这个错误:ClassCastException occurred while creating the mockito mock
。我找到了解决方案,但它不起作用。运行测试时,模拟某些 JRE 类的类中发生错误。如果我删除使用 verifyStatic () 的测试,则错误将消失。如果我单独运行测试并出现错误,则一切正常。 我试过了:
1)在Core
模块的测试文件夹中,我添加了包org.mockito.configuration
和MockitoConfigutation
。 如果我运行调试器并将停止点设置为enableClassCache()
方法,则当我运行测试时它会停止,这意味着此类按预期工作。
2)在有错误的类上添加@PowerMockIgnore({"*.*"})
注释。
有错误的示例类:
@PowerMockIgnore({"*.*"})
public class PersonTest extends AbsTest {
@BeforeClass
protected void setUp() throws Exception {
mock(ArrayList.class);
}
}
Core
模块上的build.gradle
:
...
testCompile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
testCompile "com.badlogicgames.gdx:gdx-backend-headless:$gdxVersion"
testCompile 'org.testng:testng:6.13.1'
testCompile "org.mockito:mockito-core:2.8.9"
testCompile "org.powermock:powermock-api-mockito2:1.7.3"
testCompile "org.powermock:powermock-module-testng:1.7.3"
testCompileOnly "org.projectlombok:lombok:1.16.18"
...
错误:
org.mockito.exceptions.base.MockitoException: ClassCastException 创建模拟时发生 模拟 : 模拟类 : 'com.brashmonkey.spriter.Animation',由类加载器加载: 'sun.misc.Launcher$AppClassLoader@18b4aac2' 创建了类: 'com.brashmonkey.spriter.Animation$MockitoMock$1309622717',加载者 类加载器: 'net.bytebuddy.dynamic.loading.MultipleParentClassLoader@185f7840'
代理实例类: 'com.brashmonkey.spriter.Animation$MockitoMock$1309622717',加载者 类加载器: 'net.bytebuddy.dynamic.loading.MultipleParentClassLoader@185f7840'
实例创建者:ObjenesisInstantiator您可能会遇到类加载问题,请询问模拟 邮件列表。
在 ru.coolone.adventure_emulation.scripts.person.PersonTest.setUpClass(PersonTest.java:160) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124) 在 org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:59) 在 org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:451) at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222) at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:142) 在 org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:163) 在 org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:105) at org.testng.TestRunner.privateRun(TestRunner.java:648) at org.testng.TestRunner.run(TestRunner.java:505) at org.testng.SuiteRunner.runTest(SuiteRunner.java:455) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415) at org.testng.SuiteRunner.run(SuiteRunner.java:364) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187) at org.testng.TestNG.runSuitesLocal(TestNG.java:1116) at org.testng.TestNG.runSuites(TestNG.java:1028) at org.testng.TestNG.run(TestNG.java:996) at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72) at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:127) 原因:java.lang.ClassCastException: com.brashmonkey.spriter.Animation$MockitoMock$1309622717 不能 投射到 org.mockito.internal.creation.bytebuddy.MockAccess at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMock(SubclassByteBuddyMockMaker.java:48) 在 org.powermock.api.mockito.mockmaker.PowerMockMaker.createMock(PowerMockMaker.java:50) 在 org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.createMethodInvocationControl(DefaultMockCreator.java:116) 在 org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.createMock(DefaultMockCreator.java:69) 在 org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.mock(DefaultMockCreator.java:46) at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:138) ...还有 26 个
好的,所以我有相同的行为,它是:
@PowerMockIgnore({"*.*"})
这将忽略导致问题org.mockito.*
。更具体地说明忽略的内容,例如:
@PowerMockIgnore({"org.robolectric.","安卓。", "com.sun.org.apache.xerces.internal.jaxp.*"})