单元测试主要基于 JUnit 和 Robolectric 进行。Android Studio 默认集成好了 JUnit,而 Robolectric 则须要稍稍配置一下,这里提供两种方式进行配置。html
在每一个测试类上都加上注解java
@RunWith(RobolectricTestRunner.class)
@Config(application = TestMyApplication.class, constants = BuildConfig.class, manifest = "./AndroidManifest.xml",packageName = "com.your.package")
复制代码
其中,TestMyApplication
是测试代码的初始化Application,能够把App的Application的逻辑放在这里面android
TestMyRobolectricRunner
中集中配置buildGlobalConfig
方法@RunWith(RobolectricTestRunner.class)
注解,转而使用 @RunWith(TestMyRobolectricRunner.class)
注解public class TestMyRobolectricRunner extends RobolectricTestRunner {
/** * Creates a runner to run {@code testClass}. Looks in your working directory for your AndroidManifest.xml file * and res directory by default. Use the {@link Config} annotation to configure. * * @param testClass the test class to be run * @throws InitializationError if junit says so */
public TestMyRobolectricRunner(Class<?> testClass) throws InitializationError {
super(testClass);
}
@Override
protected Config buildGlobalConfig() {
return new Config.Builder()
.setApplication(TestMyApplication.class)
.setConstants(BuildConfig.class)
.setManifest("AndroidManifest.xml")
.setPackageName("com.yongf.mypackagename")
.build();
}
}
复制代码
SharedPreferences preferences = RuntimeEnvironment.application.getSharedPreferences(PREFERENCE_KEY, Context.MODE_PRIVATE);
复制代码
好比,点击一个按钮之后在知足条件的状况下,会跳转到一个新页面。怎么测呢,能够先知足各类设置条件,而后模拟按钮点击,而后获取系统跳转的下一个 activity 是否指定的 activity。git
//按钮点击后跳转到下一个Activity
forwardBtn.performClick();
Intent expectedIntent = new Intent(sampleActivity, LoginActivity.class);
Intent actualIntent = ShadowApplication.getInstance().getNextStartedActivity();
assertEquals(expectedIntent, actualIntent);
复制代码
某些状况下,弹出Dialog。github
ShadowDialog latestDialog = ShadowApplication.getInstance().getLatestDialog();
复制代码
遗憾的是,目前只能测试是否有对话框弹出,并不能测试对话框的内容(PS: 若是观众朋友你会的话,千万别吝啬你的才华,请在评论中或者 issue 中赐教~)app
某些状况下,弹出 Toastide
Toast latestToast = ShadowToast.getLatestToast();
String textOfLatestToast = ShadowToast.getTextOfLatestToast();
复制代码
能够测试 Toast 的显示,以及 Toast 的内容单元测试
参考上述 配置 Robolectric
章节的示例代码测试
单个测试类,在[类|方法]上面添加 @Config(sdk = 21)
便可,具体的 sdk 版本本身指定ui
目前个人作法是指定 sdk 版本为21如下,由于没有动态权限,这样须要用户受权的部分就能走。但这样带来的问题就是,权限未受权的分支部分代码没有被覆盖到。
若是你有更好的作法,赶忙来告诉我吧,谢谢啦~
onActivityResult
在运行的测试用例的时候,你可能会遇到以下错误:
java.lang.RuntimeException: Method isEmpty in android.text.TextUtils not mocked. See http://g.co/androidstudio/not-mocked for details.
at android.text.TextUtils.isEmpty(TextUtils.java)
at com.example.robolectric.TextUtilsTest.testIsEmpty(TextUtilsTest.java:14)
复制代码
如何解决呢?很简单,把 Android 源码中这个类搬到测试代码目录下便可(注意要保持包名一致)
TODO