配置同时使用PowerMock和Robolectric对Android进行单元测试
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了配置同时使用PowerMock和Robolectric对Android进行单元测试相关的知识,希望对你有一定的参考价值。
后来在PowerMock官网上找到了另外一个教程,里面说使用PowerMockRule是不靠谱的,要使用PowerMock 1.6.0引入的新的@PowerMockRunnerDelegate annotation来进行配置
具体配置文件如下:
module里面的build.gradle添加依赖:
dependencies {
......
testCompile "org.robolectric:robolectric:3.0"
testCompile ‘org.mockito:mockito-core:1.10.19‘
testCompile ‘junit:junit:4.12‘
testCompile "org.powermock:powermock-module-junit4:1.6.4"
testCompile "org.powermock:powermock-module-junit4-rule:1.6.4"
testCompile "org.powermock:powermock-api-mockito:1.6.4"
testCompile "org.powermock:powermock-classloading-xstream:1.6.4"
}
对下面的 RobolectricTest 基类进行继承,就可以正常使用PowerMock和Robolectric进行测试了
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;
/**
* Base class extended by every Robolectric test in this project.
*/
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class,
sdk = 21)
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"})
public abstract class RobolectricTest {
}
以上是关于配置同时使用PowerMock和Robolectric对Android进行单元测试的主要内容,如果未能解决你的问题,请参考以下文章