我应该使用哪些 Maven 工件来导入 PowerMock?

2022-09-01 14:22:50

我需要添加哪些jar才能让PowerMock与Mockito一起工作?我有以下依赖关系:pom.xml

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-all</artifactId>
    <version>1.9.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito</artifactId>
    <version>1.4.11</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-support</artifactId>
    <version>1.4.11</version>
    <scope>test</scope>
 </dependency>

但是当我在类级别添加注释时,Eclipse找不到它,但它可以找到.我错过了什么罐子?@PrepareForTestPowerMockito


答案 1

根据PowerMock维基上的Mockito_Maven页面,请使用以下内容:

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

powermock-api-support似乎是“仅实用程序类”,您仍然需要 中提供的核心库。powermock-module-junit4


答案 2

确保您有此导入:

import org.powermock.core.classloader.annotations.PrepareForTest;

这个罐子有它:


推荐