gpt4 book ai didi

java - 使用 mockito 和 powermock 时出现 ClassCastException

转载 作者:行者123 更新时间:2023-11-29 05:20:24 24 4
gpt4 key购买 nike

在 eclipse 中运行 junit4 测试用例时它工作正常作为一个独立的应用程序,我得到以下堆栈跟踪。

java.lang.ExceptionInInitializerError:        at sun.reflect.GeneratedSerializationConstructorAccessor6.newInstance(Unknown Source)        at java.lang.reflect.Constructor.newInstance(Constructor.java:526)        at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:40)        at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:59)        at org.mockito.internal.creation.jmock.ClassImposterizer.createProxy(ClassImposterizer.java:128)        at org.mockito.internal.creation.jmock.ClassImposterizer`enter code here`.imposterise(ClassImposterizer.java:63)        at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:111)        at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:60)        at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:143)        at org.powermock.api.extension.listener.AnnotationEnabler.standardInject(AnnotationEnabler.java:84)        at org.powermock.api.extension.listener.AnnotationEnabler.beforeTestMethod(AnnotationEnabler.java:51)        at org.powermock.tests.utils.impl.PowerMockTestNotifierImpl.notifyBeforeTestMethod(PowerMockTestNotifierImpl.java:90)        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:292)        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59)        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$LastRuleTestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:148)        at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:91)        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)        at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:86)        at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)        at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33)        at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45)        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:118)        at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:104)        at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)        at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53)        at org.junit.runners.Suite.runChild(Suite.java:127)        at org.junit.runners.Suite.runChild(Suite.java:26)        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)        at org.junit.runners.ParentRunner.run(ParentRunner.java:309)        at org.junit.runner.JUnitCore.run(JUnitCore.java:160)        at org.junit.runner.JUnitCore.run(JUnitCore.java:138)        at org.junit.runner.JUnitCore.run(JUnitCore.java:117)        at testcases.net.idt.debit.ratesvr.monitor.MonitorTest.main(MonitorTest.java:392)Caused by: java.lang.ClassCastException: org.powermock.core.classloader.MockClassLoader cannot be cast to java.net.URLClassLoader

我的测试用例使用以下注释

@RunWith(PowerMockRunner.class)@PrepareForTest({Monitor.class, Client.class, java.net.URLClassLoader.class})@PowerMockIgnore( {"javax.management.*", "java.lang.*", "java.security.*", "java.net.*" })

我如何告诉它忽略 URLClasLoader?

最佳答案

要模拟的类 Monitor.class 扩展了另一个类 MonitorBase.class 在那个类中有一个静态初始化器

static {
....
}

所以当 Monitor.class 被加载时,MonitorBase.class 在它可以被“模拟”之前被实例化我删除了“扩展 MonitorBase”并为 MonitorBase.class 创建了一个 Monitor 接口(interface)

解决了问题

关于java - 使用 mockito 和 powermock 时出现 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25025694/

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