robolectric 踩坑记录
引入
testImplementation 'androidx.fragment:fragment-testing:1.1.0'
// Core library
testImplementation 'androidx.test:core:1.2.0'
// AndroidJUnitRunner and JUnit Rules
testImplementation 'androidx.test:runner:1.2.0'
testImplementation 'androidx.test:rules:1.2.0'
testImplementation 'androidx.test.ext:junit:1.1.1'
testImplementation 'androidx.test.ext:truth:1.2.0'
testImplementation 'androidx.test:monitor:1.2.0'
testImplementation 'com.google.truth:truth:1.0'
//junit
testImplementation 'junit:junit:4.12'
//mockito
testImplementation "org.mockito:mockito-core:3.1.0"
//powermock
testImplementation "org.powermock:powermock-module-junit4:2.0.2"
testImplementation "org.powermock:powermock-module-junit4-rule:2.0.2"
testImplementation "org.powermock:powermock-api-mockito2:2.0.2"
testImplementation "org.powermock:powermock-classloading-xstream:2.0.2"
//robolectric
testImplementation "org.robolectric:robolectric:4.3.1"
//mockwebserver
testImplementation 'com.squareup.okhttp3:mockwebserver:3.12.0'
testImplementation 'com.github.andrzejchm.RESTMock:android:0.3.1'
//AssertJ
testImplementation 'org.assertj:assertj-core:3.13.2'
参考文档
官方
http://robolectric.org/androidx_test/
单元测试框架的一个demo
https://github.com/simplezhli/AndroidUT
中文说明(旧
https://maxwell-nc.github.io/android/robolectricTest.html
中文说明(稍新
踩坑记录
1第一次运行,会耗时,因为要下载robo库
image.png但是这个过程漫长到,这一定是一个bug
but no solution yet,just wait,第一次的时间是真的长。
https://github.com/robolectric/robolectric/issues/3452
可以参照里面对我的回复,手工先下载jar包。
image.png之后就非常快了
image.png2.配置过时
网上的一些资料配置,完全就是过时的
image.png3.AndroidX的兼容
The given artifact contains a string literal with a package reference 'android.support.v4.content' that cannot be safely rewritten. Libraries using reflection such as annotation processors need to be updated manually to add support for androidx.
https://github.com/robolectric/robolectric/issues/4491#issuecomment-498603341
不是butterKnife的问题,不知道哪个库出问题了。
android.jetifier.blacklist=shadows
works for me
4.该框架不能支持so的测试
涉及到一个动态库的编译,但是我们这里报错的全是第三方库,尤其类似地图so
https://github.com/robolectric/robolectric/issues?q=UnsatisfiedLinkError
都会see 1171 这个单
https://github.com/robolectric/robolectric/issues/1171
一篇中文文档
5.启动fragment失败
https://stackoverflow.com/questions/54007609/robolectric-androidx-fragments-noclassdeffounderror image.png