android - 未找到 Android 单元测试依赖项
问题描述
我正在尝试为我的项目中的一个函数编写以下单元测试
import android.content.Context
import org.junit.Test
import androidx.test.core.app.ApplicationProvider
import com.adi_random.tracky.api.searchBook
import com.adi_random.tracky.models.GoodreadsBook
import com.google.gson.Gson
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Response
import java.io.IOException
import com.google.common.truth.Truth.assertThat
/**
* Created by meadi on 6/27/2020.
*/
class BookFetchTest {
/**
* Test if the Tracy API searchBook endpoint returns the expected result and gets parsed correctly
*/
val context = ApplicationProvider.getApplicationContext<Context>()
@Test
fun bookFetchResultValidation() {
val query = "Dune";
searchBook(query, context, object : Callback {
override fun onFailure(call: Call, e: IOException) {
TODO("Not yet implemented")
}
override fun onResponse(call: Call, response: Response) {
val gson = Gson();
val res = gson.fromJson<Array<GoodreadsBook>>(
response.body?.charStream(),
Array<GoodreadsBook>
::class.java
)
assertThat(res).hasLength(20);
}
})
}
}
当点击运行时,我将进入以下依赖项Unresolved reference
的:app:compileDebugUnitTestKotlin
gradle 任务:test(双击错误突出显示androidx.test.core.app.ApplicationProvider
)、common(in com.google.common.truth.Truth.assertThat
)、ApplicationProvider(in ApplicationProvider.getApplicationContext()
)和 assertThat(在assertThat
测试结束时的调用中)。
这是我的模块build.gradle
文件:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
buildFeatures {
viewBinding true
}
testOptions {
unitTests.includeAndroidResources = true
}
defaultConfig {
applicationId "com.adi_random.tracky"
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
useLibrary 'android.test.runner'
useLibrary 'android.test.base'
useLibrary 'android.test.mock'
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.squareup.okhttp3:okhttp:4.1.0'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.navigation:navigation-fragment-ktx:2.1.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.ext:truth:1.2.0'
androidTestImplementation 'com.google.truth:truth:0.42'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test:core:1.2.0'
// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
}
如您所见,我使用指令将这些依赖项添加到了我的 gradle 构建文件中androidTestImplementation
,所以我不明白为什么会抛出这些错误。有谁有想法吗?提前致谢!
解决方案
添加此依赖项:
def androidx_test_core = "1.2.0"
androidTestImplementation "androidx.test:core-ktx:$androidx_test_core"
def androidx_test_ext = "1.1.1"
androidTestImplementation "androidx.test.ext:junit-ktx:$androidx_test_ext"
def hamcrestVersion = '2.2'
testImplementation "org.hamcrest:hamcrest:$hamcrestVersion"
推荐阅读
- amazon-web-services - 我可以将 websocket 用于我的 Lambda 函数吗
- mysql - SELECT EXISTS (SELECT 1 FROM favoritelist WHERE id=:id) 是什么意思
- javascript - 无法读取文本输入的未定义属性拆分
- python - 在 Python (Mac & Linux) 中扩展内存映射文件
- progressive-web-apps - 使用 Workbox 缓存外部下载
- python - 我的 chromedriver 立即打开并崩溃
- haskell - Haskell - 尝试使用 Prelude.read 使输出类型 a 也适用于 String
- google-cloud-platform - 如何在谷歌云存储(Python)中获取 blob 的 URI
- java - 获取数组长度时出错抛出异常
- bash - 如何使用 .sh 脚本登录 Linux 中的程序?