未找到 Android 单元测试依赖项

标签 android unit-testing kotlin junit

我正在尝试为我的项目中的函数编写以下单元测试

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);
            }

        })
    }
}

点击运行时,我在 :app:compileDebugUnitTestKotlin gradle 任务中收到以下依赖项的 未解析的引用:test (双击错误突出显示 androidx. test.core.app.ApplicationProvider)、common(在 com.google.common.truth.Truth.assertThat 中)、ApplicationProvider(在 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'

}

如您所见,我使用 androidTestImplementation 指令将这些依赖项添加到我的 gradle 构建文件中,所以我不明白为什么会抛出这些错误。有人有什么主意吗?提前致谢!

编辑:这是问题的屏幕截图: enter image description here

最佳答案

添加此依赖项:

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"

关于未找到 Android 单元测试依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62633868/

相关文章:

android - 无法使用 Typeface.createFromFile() 更改 View 的字体样式

java - 尽管 url 有效,Spring Controller 测试仍失败

java - 更新到Android 3.6.1后出现错误

javascript - 在 AngularJS 事件单元测试中模拟事件对象

java - 如何在retrofit中调用动态嵌套json对象

android自定义通知不适用于约束布局

android - 如何将参数从android传递到HTML文件

android - 无法使用 Android L (API 20) 创建模拟器

android - 如何获取日期、小时、分钟以达到特定日期

c# - 使用 Moq 的 ElasticClient 的代码覆盖率