Android单元测试系列(5)-Robolectric

Android单元测试系列(1)-开篇_Chris_166的博客-CSDN博客中就提过"Robolectric用于模拟Android接口,这套框架可以直接让测试case运行于JVM,不需要Android模拟器和手机"。

说明:App的代码使用了很多Android SDK的API,而不是纯Java的API,这些Android SDK API的接口都是在android.jar中暴露的,并没有在android.jar中实现,当App代码运行在模拟器或者Android设备上面的时候,android.jar就会被替换成了模拟器或者是Android设备上面的系统实现。

一、官网

Robolectric

Configuring Robolectric | Robolectric

GitHub - robolectric/robolectric: Android Unit Testing Framework

二、Demo示例

// Gradle依赖

mockito/Powermock/robolectric之间是需要匹配版本的,否则会出现一些兼容性问题,如下组合暂未出现过兼容性问题。

dependencies {

    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.1'

    testImplementation 'junit:junit:4.+'
    testImplementation 'org.mockito:mockito-core:2.8.9'

    testImplementation "org.powermock:powermock-api-mockito2:1.7.1" // 这个mockito2必须引入,否则的话会找不到PowerMockito类
    testImplementation "org.powermock:powermock-module-junit4:1.7.1"
    testImplementation 'org.powermock:powermock-core:1.7.1'

    testImplementation "org.powermock:powermock-module-junit4-rule:1.7.1"
    testImplementation "org.powermock:powermock-classloading-xstream:1.7.1"
    testImplementation "org.robolectric:robolectric:3.3.1"


    // androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    //androidTestImplementation "org.mockito:mockito-android:4.4.0"

}

///待续/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值