kotlinx-coroutines-test依赖项似乎对我不起作用,因为我无法访问TestCoroutineDispatchersetMain()resetMain()等依赖项的成员.我遵循这doc个,但尽管添加了gradle依赖项,也无法访问成员.我试图重建项目,使缓存失效,然后重新启动,但似乎什么都不起作用.我也试过做androidExtensions {experimental = true},但还是没有成功.

build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.android.kotlincoroutines"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
androidExtensions {
    experimental = true
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1'


    implementation 'androidx.appcompat:appcompat:1.1.0-alpha02'
    implementation 'com.google.android.material:material:1.1.0-alpha03'

    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

    // ViewModel and LiveData
    def lifecycle_version = '2.0.0-beta01'
    implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0-alpha02"
    testImplementation "androidx.arch.core:core-testing:$lifecycle_version"

    // Room for database
    def room_version = '2.0.0'
    implementation "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:$room_version"

    def work_version = "1.0.0-rc01"
    implementation "android.arch.work:work-runtime:$work_version"

    androidTestImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test:rules:1.1.1'
    androidTestImplementation "com.google.truth:truth:0.42"
    androidTestImplementation "androidx.arch.core:core-testing:$lifecycle_version"

    testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.0-M1'
}

Where am I going wrong here?

推荐答案

将库版本更改为1.3.2而不是1.3.0-M1,如下所示:

testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.2'

And be sure that your tests are unit test in the test folder, not instrumented test in androidTest folder.

Kotlin相关问答推荐

如何将时间值格式化为00:00和00:00:00 Kotlin?""""

房间数据库操作中的协程取消

为什么可组合对象看似无状态(唯一传递的参数是函数,而不是状态),但会进行重组

JetPack Compose:添加点击持续时间

为 Gradle 子项目配置 Kotlin 扩展

Kotlin 日期格式从一种更改为另一种

如何为 material.Slider 视图创建绑定适配器?

为什么 IntelliJ Idea 无法识别我的 Spek 测试?

runBlocking 中的 deferred.await() 抛出的异常即使在被捕获后也被视为未处理

Kotlin 和 Java 字符串拆分与正则表达式的区别

Kotlin Native如何将字节数组转换为字符串?

在 Kotlin 中创建 Spinner 时,如何在 Fragment 中的旋转屏幕上修复指定为非空的参数为空?

重复构建失败To use Coroutine features, you must add `ktx`......

在kotlin中初始化类变量的正确位置是什么

如何从协程范围返回值

如何在Kotlin中将字符串转换为InputStream?

kotlin中的全局可拓函数

@StringRes、@DrawableRes、@LayoutRes等android注释使用kotlin参数进行判断

递归方法调用在 kotlin 中导致 StackOverFlowError 但在 java 中没有

Kotlin 是否支持部分应用程序?