問題描述
我正在嘗試使用 PowerMockito
模擬構(gòu)造函數(shù),但每次運行測試時都會出現(xiàn)以下錯誤:
I'm trying to mock a constructor using PowerMockito
but every time I run the test I get the following error:
java.lang.NoSuchMethodError: org.mockito.internal.creation.MockSettingsImpl.setMockName(Lorg/mockito/mock/MockName;)Lorg/mockito/internal/creation/settings/CreationSettings;
at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:107)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:60)
at org.powermock.api.mockito.internal.expectation.DefaultConstructorExpectationSetup.createNewSubstituteMock(DefaultConstructorExpectationSetup.java:105)
at org.powermock.api.mockito.internal.expectation.DefaultConstructorExpectationSetup.withAnyArguments(DefaultConstructorExpectationSetup.java:71)
我的項目中有以下 PowerMock 依賴項:
I have the following PowerMock dependencies in my project:
- org.powermock:powermock-module-junit4:1.5.6
- org.powermock:powermock-mockito-release-full:1.5.6
我已經(jīng)跟蹤了我的項目的依賴樹并修復(fù)了沖突,以便 mockito-all:1.9.5
被包含在構(gòu)建中.
I've traced the dependency tree of my project and fixed conflicts so that mockito-all:1.9.5
gets included in the build.
推薦答案
我的問題是由于我的項目(傳遞)依賴項中的 javassist 版本沖突.我所做的是搜索將舊版本的 javassist 放入構(gòu)建中的所有依賴項,然后排除它們.例如:
My problem was due to conflicting versions of javassist in my project's (transitive) dependencies. What I did was search for all dependencies that put old version of javassist in the build, then exclude them. For example:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.5.1-Final</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>
這篇關(guān)于PowerMock 拋出 NoSuchMethodError (setMockName)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!