【发布时间】:2019-05-05 21:34:18
【问题描述】:
我正在尝试在 Android Studio 中的 Android 项目的单元测试中使用 MockContext。问题是,包android.test.* 在项目中不可见。
我不确定我应该向 Gradle 添加什么才能导入它。我试过com.android.support.test:rules:1.0.2 和androidx.test:rules:1.1.1(IDE 提出的建议之一),但这不是我要找的。p>
build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.myApp"
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.firebase:firebase-core:16.0.8'
implementation 'com.android.support:recyclerview-v7:28.0.0'
testImplementation 'com.android.support.test:rules:1.0.2'
}
apply plugin: 'com.google.gms.google-services'
编译时的错误信息:
错误:包 android.test.mock 不存在
我应该在 gradle 中添加什么才能访问 android.test 包?
【问题讨论】:
-
您使用
implementation来表示该依赖项。试试testImplementation。 -
@CommonsWare 这没有帮助,但感谢您了解:) 已编辑。
-
这里有同样的问题!奇怪的是,Siena 的答案不适用于我的 库模块 的测试...然后在我的 应用程序模块 的 build.gradle 中添加
useLibrary 'android.test.mock'修复了失败进口!!!在我的 库模块 测试中。奇怪...
标签: android unit-testing