如何在 Android Studio 中使用 android.test.mock [英] How to use android.test.mock in Android Studio
问题描述
我正在尝试在单元中使用 MockContext在 Android Studio 中对 Android 项目进行测试.问题是,包 android.test.*
在项目中不可见.
我不确定应该向 Gradle 添加什么才能导入它.我尝试了 com.android.support.test:rules:1.0.2
和 androidx.test:rules:1.1.1
(IDE 提出的建议之一),但是这不是我要找的.p>
build.gradle:
应用插件:'com.android.application'安卓 {compileSdkVersion 28默认配置{applicationIdcom.myApp"minSdk 版本 23目标SDK版本28版本代码 1版本名称1.0"testInstrumentationRunnerandroid.support.test.runner.AndroidJUnitRunner"}构建类型{释放 {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'}}}依赖{实现文件树(目录:'libs',包括:['*.jar'])//不检查GradleCompatible实现 'com.android.support:appcompat-v7:28.0.0'实现 'com.android.support:design:28.0.0'实现 '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'实现 'com.google.firebase:firebase-core:16.0.8'实现 'com.android.support:recyclerview-v7:28.0.0'testImplementation 'com.android.support.test:rules:1.0.2'}应用插件:'com.google.gms.google-services'
编译时报错:
<块引用>错误:android.test.mock 包不存在
我应该向 gradle 添加什么才能访问 android.test
包?
实际上,您可以从单元测试中访问 android.test.mock
并获得对 MockContext
班级.对于 AndroidX 项目,您可以在应用模块的 build.gradle
中添加以下内容:
android {...useLibrary 'android.test.mock'}
来源:https://developer.android.com/training/testing/设置项目
I'm trying to use MockContext in unit tests for Android projects in Android Studio. The problem is, package android.test.*
is not visible in the project.
I'm not sure what should I add to Gradle in order to import it. I tried com.android.support.test:rules:1.0.2
and androidx.test:rules:1.1.1
(one of the suggestions made by IDE), but that's not the one I'm looking for.
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'
Error message when compiling:
error: package android.test.mock does not exist
What should I add to gradle to have access to android.test
packages?
Actually, you can access android.test.mock
from your unit tests and gain access to the MockContext
class. For AndroidX projects, you can add the following in your app module's build.gradle
:
android {
...
useLibrary 'android.test.mock'
}
Source: https://developer.android.com/training/testing/set-up-project
这篇关于如何在 Android Studio 中使用 android.test.mock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!