将Powermock与Mockito集成-java.lang.NoSuchMethodError

时间:2019-09-11 08:27:45

标签: junit mockito powermock

我正在尝试使用以下依赖项来集成Mockito,JUnit和Powermock:

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-core</artifactId>
  <version>1.10.19</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.powermock</groupId>
  <artifactId>powermock-module-junit4</artifactId>
  <version>1.6.4</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.powermock</groupId>
  <artifactId>powermock-api-mockito</artifactId>
  <version>1.6.4</version>
  <scope>test</scope>
</dependency>

运行IT测试时出现以下错误:

java.lang.NoSuchMethodError: org.mockito.MockingDetails.getMockCreationSettings()Lorg/mockito/mock/MockCreationSettings;

我花了很长时间找出合适的工件版本,但在对其进行了更改并修复了一个错误之后,出现了一个错误:(

1 个答案:

答案 0 :(得分:1)

您的依赖版本太旧了。 我建议选择最新的2.x版本:

<properties>
  ...

  <version.mockito>2.23.4</version.mockito>
  <version.powermock>2.0.2</version.powermock>
</properties>

<dependencies>
   ...

  <dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>${version.mockito}</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>${version.powermock}</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito2</artifactId>
    <version>${version.powermock}</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-core</artifactId>
    <version>${version.powermock}</version>
    <scope>test</scope>
  </dependency>
</dependencies>

这来自一个正在运行的项目,如果您继续遇到错误,请扩展问题,包括Java代码。