unit-testing - 无法导入 kotlin.test 以使用 assertFailsWith

标签 unit-testing android-studio kotlin

对于我的单元测试,我需要导入 kotlin.test 才能使用 assertFailsWith。

我相信我的 app build.grade 文件中有正确的文件。该文件的一部分是:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.vectordrawable:vectordrawable:1.0.1'
    implementation 'androidx.navigation:navigation-fragment:2.0.0'
    implementation 'androidx.navigation:navigation-ui:2.0.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.0.0'
    implementation 'androidx.navigation:navigation-ui-ktx:2.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

    // ----
    // Per https://developer.android.com/training/testing/unit-testing/local-unit-tests
    //
    // Required -- JUnit 4 framework
    testImplementation 'junit:junit:4.12'
    // Optional -- Robolectric environment
    testImplementation 'androidx.test:core:1.0.0'
    // Optional -- Mockito framework
    testImplementation 'org.mockito:mockito-core:1.10.19'
    // ----


    testImplementation "org.jetbrains.kotlin:kotlin-test-common:$kotlin_version"
    testImplementation "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlin_version"
}

我不确定我错过了什么......

最佳答案

对于 Android 项目和 JUnit 测试,您需要使用 kotlin-test-junit 模块而不是 kotlin-test-commonkotlin-test-annotations-common – 适用于多平台项目中平台之间共享的代码,在 JVM/Android 模块中被忽略。

用这个替换这两个依赖项:

testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"

关于unit-testing - 无法导入 kotlin.test 以使用 assertFailsWith,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59328918/

相关文章:

c# - Win10 UWP、Prism/Unity 和单元测试以及类库、静默失败

javascript - 如何模拟使用在 Node 模块中导出的导入(ES6 typescript)进行单元测试的外部注入(inject)库

python - 带有测试套件和测试用例的 TestSuite

Android Studio Gradle 构建风格

android - 为什么没有通过回收站 View 测试?

android - 在 Kotlin 的内部类中使用 fragment 的上下文

android - Android 单元测试的奇怪行为

java - 从 SurfaceView 创建类时出错,该类将作为线程运行

android-studio - 如何动态生成 Java 源代码以在 Android Studio 中使用 Gradle 进行构建?

android - 使用 dagger2 时未生成 DataBindingComponent