How to use PowerMock on android sdk

…衆ロ難τιáo~ 提交于 2019-12-11 02:44:43

问题


I want to write some unit tests and instrumentation tests for my android project.

However, I've met a problem that stuck me a while... I need to mock a static method and fake the return value to test the project.

After survey from some forums, the only way to do this is to use PowerMock to mock the static method.

This is a part of my gradle:

androidTestCompile "org.powermock:powermock-module-junit4:1.7.0"
androidTestCompile "org.powermock:powermock-module-junit4-rule:1.7.0"
androidTestCompile "org.powermock:powermock-api-mockito:1.7.0"

and my test class:

@RunWith(PowerMockRunner.class)
public class MyClassTest {
    ...
}

Then I got "java.lang.NoClassDefFoundError: Failed resolution of: Ljava/beans/Introspector;" in PowerMock...

It seems android sdk had removed some Class in java.beans.* https://developer.android.com/reference/java/beans/package-summary.html

How can I mock or fake the static method return values?

Thanks a lot.


回答1:


Try going with an older version

def powermock_version="1.6.6" androidTestCompile "org.powermock:powermock-module-junit4:$powermock_version" androidTestCompile "org.powermock:powermock-module-junit4-rule:$powermock_version" androidTestCompile "org.powermock:powermock-api-mockito:$powermock_version"



来源:https://stackoverflow.com/questions/45626317/how-to-use-powermock-on-android-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!