在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设备上面的系统实现。
Configuring Robolectric | Robolectric
GitHub - robolectric/robolectric: Android Unit Testing Framework
// 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"
}
///待续/