Java PowerMock 抛出 NoSuchMethodError (setMockName)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27136752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:53:08  来源:igfitidea点击:

PowerMock throws NoSuchMethodError (setMockName)

javamockitojunit4powermock

提问by Psycho Punch

I'm trying to mock a constructor using PowerMockitobut every time I run the test I get the following error:

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

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)

I have the following PowerMock dependencies in my project:

我的项目中有以下 PowerMock 依赖项:

  • org.powermock:powermock-module-junit4:1.5.6
  • org.powermock:powermock-mockito-release-full:1.5.6
  • org.powermock:powermock-module-junit4:1.5.6
  • org.powermock:powermock-mockito-release-full:1.5.6

I've traced the dependency tree of my project and fixed conflicts so that mockito-all:1.9.5gets included in the build.

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

采纳答案by Psycho Punch

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:

我的问题是由于我的项目(传递)依赖项中 javassist 的版本冲突。我所做的是搜索所有将旧版本 javassist 放入构建中的依赖项,然后排除它们。例如:

<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>

回答by ununiform

For me, in Eclipse, the fix to this problem was found in Java Build Path. Click on Order and Export tab. Move Web App Libraries to bottom. Note, that when appropriate, this will also allow you to view source of 3rd party libraries when Eclipse tells you that source cannot be found.

对我来说,在 Eclipse 中,可以在 Java Build Path 中找到解决此问题的方法。单击订购和导出选项卡。将 Web 应用程序库移至底部。请注意,在适当的时候,这也将允许您在 Eclipse 告诉您无法找到源代码时查看 3rd 方库的源代码。

回答by Abhijeet

I had

我有

org.mockito mockito-all 1.8.4

org.mockito mockito-all 1.8.4

added to my pom.xml apart from powermock's dependecies, removing this worked for me.

除了powermock的依赖之外,添加到我的pom.xml中,删除它对我有用。

回答by Vitaly

In my case it was a conflict dependency. I had fix it after exclude mockito-core artifact:

就我而言,这是一种冲突依赖。我在排除 mockito-core 工件后修复了它:

 <dependency>
        <groupId>com.googlecode.catch-exception</groupId>
        <artifactId>catch-exception</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-core</artifactId>
            </exclusion>
        </exclusions>
        <version>1.0.4</version>
        <scope>test</scope>
    </dependency>

回答by prayagupd

Make sure powermockitoand mockitoversions are alignedas in this versions chart - MockitoUsage#supported-versions,

确保powermockitomockito版本排列在这个版本中的图表- MockitoUsage#支持的版本

Mockito                     | PowerMock
------------------------------------------------------------------------------
2.0.0-beta - 2.0.42-beta    |   1.6.5+
------------------------------------------------------------------------------
1.10.19                     |   1.6.4
1.10.8 - 1.10.x             |   1.6.2+
1.9.5-rc1 - 1.9.5           |   1.5.0 - 1.5.6
1.9.0-rc1 & 1.9.0           |   1.4.10 - 1.4.12
1.8.5                       |   1.3.9 - 1.4.9
1.8.4                       |   1.3.7 & 1.3.8
1.8.3                       |   1.3.6
1.8.1 & 1.8.2               |   1.3.5
1.8                         |   1.3
1.7                         |   1.2.5

Easy way to find mockitoand powermock-mockitoversion using maven is,

使用 Maven查找mockitopowermock-mockito版本的简单方法是,

mvn dependency:tree | grep mockito
[INFO] |  \- org.mockito:mockito-core:jar:1.8.5:compile
[INFO] +- org.mockito:mockito-all:jar:1.8.5:compile
[INFO] +- org.powermock:powermock-api-mockito:jar:1.4.9:compile

Problem could be the conflicting versions of mockitoin the application and the one that powermockitouses, conflicting as below in my case where I'm using powermock 1.6.5which does not support mockito 1.8.5

问题可能是相互冲突的版本mockito在应用程序和一个powermockito用途,冲突如下面我的情况下我使用的是powermock 1.6.5不支持mockito 1.8.5

mvn clean dependency:tree | grep mockito
[INFO] +- org.mockito:mockito-all:jar:1.8.5:compile

[INFO] \- org.powermock:powermock-api-mockito:jar:1.6.5:compile
[INFO]    +- org.mockito:mockito-core:jar:1.10.19:compile
[INFO]    \- org.powermock:powermock-api-mockito-common:jar:1.6.5:compile

回答by Farlon Souto

Also, consider removing PowerMock, bytebuddy and objenesis dependencies at all. Keep only the following Mockito dependency.

另外,请考虑完全删除 PowerMock、bytebuddy 和 objenesis 依赖项。仅保留以下 Mockito 依赖项。

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>2.7.12</version>
        <scope>test</scope>
    </dependency>

This should fix the error for the most part of the scenarios.

这应该可以修复大多数情况下的错误。

回答by Ayu

while migrating springboot from 1.5 to 2.0.7 The versions of mockito in springboot and powermock are different so explicitly give mockito dependency

将 springboot 从 1.5 迁移到 2.0.7 springboot 和 powermock 中的 mockito 版本不同,因此明确给出了 mockito 依赖

This is compatible while migrating to springboot 2.0.7

这在迁移到 springboot 2.0.7 时是兼容的

testCompile "org.powermock:powermock-api-mockito2:${powermockVersion}"
testCompile group: 'org.mockito', name: 'mockito-core', version: '2.8.9'
testCompile "org.powermock:powermock-module-junit4:1.7.3"
testCompile "org.powermock:powermock-core:1.7.3"