我正在尝试使用模拟上下文在 Android Studio 中对 Android 项目进行单元测试。问题是,包android.test.*
在项目中不可见。
我不确定应该向 Gradle 添加什么才能导入它。我试过com.android.support.test:rules:1.0.2
and androidx.test:rules:1.1.1
(IDE 提出的建议之一),但这不是我正在寻找的。
构建.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
包裹?
其实你可以访问android.test.mock
从您的单元测试中获取访问权限MockContext
班级。对于 AndroidX 项目,您可以在应用程序模块中添加以下内容build.gradle
:
android {
...
useLibrary 'android.test.mock'
}
Source: https://developer.android.com/training/testing/set-up-project
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)