Android 单元测试之Roboletric 环境配置

Android 单元测试之Roboletric 环境配置

Android 单元测试之JUnit和Mockito
Android 单元测试之Roboletric 环境配置
Android 单元测试之Roboletric的简单使用
Android 单元测试之Roboletric RxJava、Retrofit、访问真实网络、虚拟服务器
Android 单元测试之Espresso - Google官方UI测试框架

Robolectric由于只在Java虚拟机中运行,速度很快,虽然在API支持上无法和Espresso相比,但速度有很大优势,适合单元测试,尤其是TDD时使用。

官网: http://robolectric.org/activity-lifecycle/
GitHub:https://github.com/robolectric/robolectric

虽然官网上有相关的教程,但是却有很多坑,表示一脸懵逼。。

按官网上进行配置

添加

testCompile 'org.robolectric:robolectric:3.1.2'  

运行单元测试出现了一下错误:

错误一
NoClassDefFoundError:javax / microedition khronos / opengl / GL

解决方法:

这个错误只在Android6.0+上出现,可通过@Config进行配置测试的SDK

@Config(constants = BuildConfig.class, sdk = 21)
错误二
java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Description;)V

解决方法:

添加

testCompile 'org.khronos:opengl-api:gl1.1-android-2.1_r1'
testCompile 'org.hamcrest:hamcrest-all:1.3'
错误三
Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app (23.1.1)  
and test app (22.2.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.

解决方法:

android{
    ...
    configurations.all {
        resolutionStrategy {
            force 'com.android.support:support-annotations:23.2.1'
        }
    }   
}
错误四
assertThat Intent 时 无论如何都匹配不成功

解决方法:

暂时使用这个

/**
 * 由于robolectric:3.1.X在assetThat Intent时的bug导致比较不成功,故暂时使用该方法
 *
 * @param expected
 * @param actual
 */
public static void assertIntent(Intent expected, Intent actual) {
    assertEquals(expected.toString(), actual.toString());
}
错误五

这个错误在robolectric3.0.0及之前出现

error: cannot access AndroidHttpClient

解决方法:

android{
    ...
    useLibrary 'org.apache.http.legacy' 
}

其他

源码Demo
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

氦客

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值