gpt4 book ai didi

unit-testing - 使用 PowerMock 后的 Mockito ClassCastException

转载 作者:行者123 更新时间:2023-12-04 17:46:37 25 4
gpt4 key购买 nike

successfully integrating PowerMock into my project 之后,我遇到了这个错误:ClassCastException occurred while creating the mockito mock。我找到了一个 solution , 但它不起作用。运行测试时,模拟某些 JRE 类的类中发生错误。如果删除使用 verifyStatic () 的测试,则错误消失。如果我单独运行有错误的测试,一切正常。我试过:

1) 在 Core 模块的 tests 文件夹中,我添加了包 org.mockito.configurationMockitoConfigutation。如果我运行调试器并将停止点设置为 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 occurred while creating the mockito mock : class to mock : 'com.brashmonkey.spriter.Animation', loaded by classloader : 'sun.misc.Launcher$AppClassLoader@18b4aac2' created class : 'com.brashmonkey.spriter.Animation$MockitoMock$1309622717', loaded by classloader : 'net.bytebuddy.dynamic.loading.MultipleParentClassLoader@185f7840'
proxy instance class : 'com.brashmonkey.spriter.Animation$MockitoMock$1309622717', loaded by classloader : 'net.bytebuddy.dynamic.loading.MultipleParentClassLoader@185f7840'
instance creation by : ObjenesisInstantiator

You might experience classloading issues, please ask the mockito mailing-list.

at 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) at 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) at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:59) at 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) at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:163) at 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.runSuitesLocally(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) Caused by: java.lang.ClassCastException: com.brashmonkey.spriter.Animation$MockitoMock$1309622717 cannot be cast to org.mockito.internal.creation.bytebuddy.MockAccess at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMock(SubclassByteBuddyMockMaker.java:48) at org.powermock.api.mockito.mockmaker.PowerMockMaker.createMock(PowerMockMaker.java:50) at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.createMethodInvocationControl(DefaultMockCreator.java:116) at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.createMock(DefaultMockCreator.java:69) at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.mock(DefaultMockCreator.java:46) at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:138) ... 26 more

最佳答案

好的,所以我有同样的行为,就是这条线:

@PowerMockIgnore({"*.*"})

这将忽略导致问题的 org.mockito.*。更具体地说明被忽略的内容,例如:

@PowerMockIgnore({"org.robolectric.", "android.","com.sun.org.apache.xerces.internal.jaxp.*"})

关于unit-testing - 使用 PowerMock 后的 Mockito ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48243051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com