标签:
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 }
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 static org.powermock.api.mockito.PowerMockito.mock; import static org.powermock.api.mockito.PowerMockito.spy; import static org.powermock.api.mockito.PowerMockito.when;
@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; }
配置同时使用PowerMock和Robolectric对Android进行单元测试
标签:
原文地址:http://www.cnblogs.com/cmicat/p/5206835.html