PowerMock 抛出 NoSuchMethodError (setMockName)

2022-01-14 00:00:00 mockito java junit4 powermock

我正在尝试使用 PowerMockito 模拟构造函数,但每次运行测试时都会出现以下错误:

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

我已经跟踪了我的项目的依赖树并修复了冲突,以便 mockito-all:1.9.5 被包含在构建中.

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 放入构建中的所有依赖项,然后排除它们.例如:

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>

相关文章