配置同时使用PowerMock和Robolectric对Android进行单元测试
后来在PowerMock官网上找到了另外一个教程,里面说使用PowerMockRule是不靠谱的,要使用PowerMock 1.6.0引入的新的@PowerMockRunnerDelegate annotation来进行配置
具体配置文件如下:
module里面的build.gradle添加依赖:
1 dependencies { 2 ...... 3 4 testCompile "org.robolectric:robolectric:3.0" 5 testCompile 'org.mockito:mockito-core:1.10.19' 6 testCompile 'junit:junit:4.12' 7 testCompile "org.powermock:powermock-module-junit4:1.6.4" 8 testCompile "org.powermock:powermock-module-junit4-rule:1.6.4" 9 testCompile "org.powermock:powermock-api-mockito:1.6.4" 10 testCompile "org.powermock:powermock-classloading-xstream:1.6.4" 11 }
对下面的 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. * <p/> * You can use Powermock together with Robolectric. */ @RunWith(PowerMockRunner.class) @PowerMockRunnerDelegate(RobolectricGradleTestRunner.class) @Config(constants = BuildConfig.class, sdk = 21) @PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"}) public abstract class RobolectricTest { }
记住要import Powermock版本的mockito api,不然会出现稀奇古怪的bug。
比如
import static org.powermock.api.mockito.PowerMockito.mock; import static org.powermock.api.mockito.PowerMockito.spy; import static org.powermock.api.mockito.PowerMockito.when;
有时候Robolectric会提示你不能找到 Manifest.xml ,这时候一般clean project就可以了。
Robolectric 3.0 暂时不支持加载jniLibs,如果你的代码用到了jniLibs,就只能使用 Android Instrumentation Tests了,如果你像我一样 extends android.app.Application 来创建自己的Application类,并在该Application类中写了了需要调用jniLibs的代码,可以使用以下的方法在unit test中跳过这些代码。
@Override public void onCreate() { super.onCreate(); if (isJUnitTest()) { // 省略掉包含jniLibs的library的初始化 } else { // 进行正常的初始化 } } public static boolean isJUnitTest() { StackTraceElement[] stackTrace = new Throwable().getStackTrace(); for (StackTraceElement element : stackTrace) { if (element.getClassName().startsWith("org.junit.")) { return true; } } return false; }