【问题标题】:Android Unit Testing dependencies not found未找到 Android 单元测试依赖项
【发布时间】:2020-06-29 08:16:23
【问题描述】:

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

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 任务中获得了以下依赖项的Unresolved reference:测试(双击错误突出显示androidx.test.core.app.ApplicationProvider),常见(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 构建文件中,所以我不明白为什么会抛出这些错误。有人有什么主意吗?提前致谢!

编辑:这是问题的截图:

【问题讨论】:

  • for assertThat: 删除上次导入 import com.google.common.truth.Truth.assertThat 并导入 hamcrest 导入。
  • 做到了。这修复了与断言相关的错误,但我仍然得到了另外 2 个
  • 为什么在 gradle 中添加这一行:`useLibrary 'android.test.runner' useLibrary 'android.test.base' useLibrary 'android.test.mock' `。删除此内容并重试
  • 替换您的屏幕截图。我想看看你的进口。
  • 上面的都是我的导入

标签: android unit-testing kotlin junit


【解决方案1】:

添加这个依赖:

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"

【讨论】:

  • 我添加了这个依赖,但仍然得到同样的错误。
  • 请插入未解决依赖关系的代码截图
  • 我刚刚添加了截图
  • 作为最后的解决方案:检查该项目的 gradle:github.com/makazemi/Mvvm-Android-Testing-Sample/tree/develop。测试内容的所有依赖项都在其中可用。删除您的测试依赖项并过去这些。
  • 好的,谢谢。错误消失了。我真的很好奇问题出在哪里,因为仅复制缺少的依赖项不起作用,我真的不得不用您提供的项目中的依赖项替换我的所有依赖项。但是现在我收到此错误消息:`java.lang.IllegalStateException:未注册仪器!必须在注册仪器下运行。在 androidx.test.platform.app.InstrumentationRegistry.getInstrumentation(InstrumentationRegistry.java:45) 在 androidx.test.core.app.ApplicationProvider.getApplicationContext(ApplicationProvider.java:41)`
猜你喜欢
  • 1970-01-01
  • 2018-03-05
  • 2011-12-21
  • 1970-01-01
  • 2015-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多