gpt4 book ai didi

java - PowerMock 和 Mockito NoSuchMethodError

转载 作者:行者123 更新时间:2023-12-04 13:00:06 28 4
gpt4 key购买 nike

我想模拟静态方法。

@PrepareForTest({PersistenceCookieThreadLocal.class})
@RunWith(PowerMockRunner.class)
public class UserBcImplTest {}

我收到以下错误:

java.lang.NoSuchMethodError: org.mockito.internal.creation.MockSettingsImpl.getName()Ljava/lang/String;



依赖:
<dependency org="junit" name="junit" rev="4.12"/>
<dependency org="org.mockito" name="mockito-core" rev="2.8.9"/>
<dependency org="org.powermock" name="powermock-module-junit4" rev="1.7.4"/>
<dependency org="org.powermock" name="powermock-api-mockito2" rev="1.7.4"/>

我已经阅读了很多文章,但没有一篇对我有用。

堆栈跟踪:
org.mockito.internal.creation.MockSettingsImpl.getName()Ljava/lang/String;" type="java.lang.NoSuchMethodError">java.lang.NoSuchMethodError: org.mockito.internal.creation.MockSettingsImpl.getName()Ljava/lang/String;
at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.toInstanceName(DefaultMockCreator.java:149)
at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.createMock(DefaultMockCreator.java:58)
at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.mock(DefaultMockCreator.java:46)
at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:193)
at org.powermock.api.extension.listener.AnnotationEnabler.standardInject(AnnotationEnabler.java:107)
at org.powermock.api.extension.listener.AnnotationEnabler.beforeTestMethod(AnnotationEnabler.java:55)
at org.powermock.tests.utils.impl.PowerMockTestNotifierImpl.notifyBeforeTestMethod(PowerMockTestNotifierImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:308)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:131)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$TestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:147)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.evaluateStatement(PowerMockJUnit47RunnerDelegateImpl.java:107)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:298)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:218)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:160)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:134)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:136)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:121)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)

最佳答案

确保版本对齐,这意味着您必须将 powermock 2.x 版本用于 mockito 2.8.9+。
还要确保您没有加载两个不同版本的相同 jar。

https://github.com/powermock/powermock/wiki/Mockito#supported-versions

Mockito       PowerMock
2.8.9+ 2.x
2.8.0-2.8.9 1.7.x
2.7.5 1.7.0RC4

关于java - PowerMock 和 Mockito NoSuchMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59430391/

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