PowerMock没有找到PrepareForTest注释

时间:2017-12-31 08:29:00

标签: junit mockito powermock

我正在编写Java类的测试用例,我第一次使用PowerMock框架。我添加到我的pom.xml的依赖项如下所示,以及完整的错误消息。当我在我的测试类中尝试使用@PrepareForTest注释时,它没有导入任何内容。我检查了powermock-core JAR内部,但没有发现任何对应的内容 import org.powermock.core.classloader.annotations.PrepareForTest;

我关注了Which Maven artifacts should I use to import PowerMock?,但它没有用。

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>           
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.10.19</version>
        <scope>test</scope>
    </dependency>
   <dependency>
         <groupId>org.powermock</groupId>
         <artifactId>powermock-api-support</artifactId>
         <version>1.7.3</version>
   </dependency>
   <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito2</artifactId>
        <version>1.7.3</version>
        <scope>test</scope>
    </dependency>    
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>1.7.3</version>
        <scope>test</scope>
    </dependency>
</dependencies>
  

java.lang.NoSuchMethodError:org.mockito.internal.handler.MockHandlerFactory.createMockHandler(Lorg / mockito / mock / MockCreationSettings;)Lorg / mockito / i nternal / InternalMock Handler;在org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.createMethodI nvocationControl(Def aultMockCreator.java:114)

1 个答案:

答案 0 :(得分:0)

我尝试了你的代码,它对我有用。 你试过构建一个应用程序吗?

mvn clean install

您应该检查是否要导入测试:

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

第二个错误: 你应该使用powermock版本1.7.1和junit 4.4或更高版本。

https://github.com/powermock/powermock/wiki/Mockito-2-Maven

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-support</artifactId>
            <version>1.7.1</version>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito2</artifactId>
            <version>1.7.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>1.7.1</version>
            <scope>test</scope>
        </dependency>