运行测试时Robolectric PowerMock java.lang.NoClassDefFoundError错误

时间:2017-09-06 19:54:44

标签: java android mockito powermock robolectric

我想集成powermock来测试firebase逻辑。我试着运行测试并得到了这个: java.lang.NoClassDefFoundError: org/mockito/cglib/proxy/MethodInterceptor

这是我的app.gradle:

testCompile 'junit:junit:4.12'
    testCompile "org.powermock:powermock-module-junit4:1.7.0"
    testCompile "org.powermock:powermock-module-junit4-rule:1.7.0"
    testCompile "org.powermock:powermock-api-mockito:1.7.0"
    testCompile "org.powermock:powermock-classloading-xstream:1.7.0"
    testCompile "org.robolectric:robolectric:3.4.2"
    testCompile 'org.mockito:mockito-core:2.1.0'

这是我的测试:

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class)
@PowerMockIgnore({ "org.mockito.*", "org.robolectric.*", "android.*" })
@PrepareForTest({FirebaseAuth.class, FirebaseDatabase.class})
public class LoginTest {

    @Before
    public void setUp() {
        PowerMockito.mockStatic(FirebaseAuth.class);
        Mockito.when(FirebaseAuth.getInstance()).thenReturn(Mockito.mock(FirebaseAuth.class));
    }

    @Test
    public void test() {

    }
}

2 个答案:

答案 0 :(得分:2)

您正在'org.mockito:mockito-core:2.1.0'使用"org.powermock:powermock-api-mockito:1.7.0"

要将PowerMock与Mockito 2一起使用,应使用"org.powermock:powermock-api-mockito2:1.7.0"

答案 1 :(得分:1)

您需要在类路径中使用 cglib 库。

我的解决方案是始终下载"完成"来自here的powermock ZIP。这些ZIP文件包含为了开始使用PowerMock所需的一切