PowerMockito给出NoClassDefFoundError

时间:2018-03-06 11:46:11

标签: android unit-testing mockito powermockito

我试图与Mockito和PowerMockito进行单元测试,但它会抛出:

Exception in thread "main" java.lang.NoClassDefFoundError: org/mockito/exceptions/Reporter

每当我尝试运行测试时。这些是我的依赖:

testCompile 'org.mockito:mockito-core:2.8.9'
testCompile 'org.powermock:powermock-api-mockito2:1.6.5'
testCompile 'org.powermock:powermock-module-junit4:1.7.4'

有谁知道如何修复它?

2 个答案:

答案 0 :(得分:1)

您似乎正在尝试使用不兼容的版本。

根据此table,您需要使用Powermock的1.7.x版本才能与您的Mockito版本2.8.9兼容。

答案 1 :(得分:0)

在您的external-dependencies.xml-

中添加这些依赖项
<dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-support</artifactId>
        <version>1.5.6</version>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-reflect</artifactId>
        <version>1.5.6</version>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito</artifactId>
        <version>1.5.6</version>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>1.5.6</version>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4-common</artifactId>
        <version>1.5.6</version>
    </dependency>

说明-

  1. powermock 1.5.6的{​​{1}}问题-需要junit 4.12:在类中找不到字段'fTestClass'

  2. powermock 1.6.1 :: org.powermock.reflect.exceptions.FieldNotFoundException需要更多当前的 mockito ::在类层次结构中找不到与添加的名称匹配的方法 org.junit.internal.runners.MethodValidator powermock 1.6.1接口。

  3. 切换到org.mockito.internal.util.MockitoSpy,同时将模拟升级到powermock 1.6.2 ::结果1.10.8java.lang.NoClassDefFoundError                 切换到org/mockito/internal/creation/util/MockitoMethodProxymockito-core 1.10.19