Getting error on using classes from kotlinx-coroutine-test

kotlinx coroutines test library
viewmodelscope unit test
testing kotlin coroutines
unresolved reference: viewmodelscope
coroutine test dispatchers io

It seems like kotlinx-coroutines-test dependency is not working for me as I can't access members of the dependency like TestCoroutineDispatcher, setMain(), resetMain() etc. I was following this doc but can't access the members despite adding the gradle dependency. I tried rebuilding the project and invalidating the cache and restart but nothing seems to work. I have also tried doing androidExtensions {experimental = true} but still no luck.


apply plugin: ''

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

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

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    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 ''

    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 "$room_version"
    kapt "$room_version"

    def work_version = "1.0.0-rc01"
    implementation "$work_version"

    androidTestImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test:rules:1.1.1'
    androidTestImplementation ""
    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?

Change your library version to 1.3.2 instead of 1.3.0-M1 like so:

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.

kotlinx-coroutines-test, class SomeTest { private val mainThreadSurrogate To test regular suspend functions or coroutines started with launch or async use the runBlockingTest� Edit Page Coroutines for asynchronous programming and more. Asynchronous or non-blocking programming is the new reality. Whether we're creating server-side, desktop or mobile applications, it's important that we provide an experience that is not only fluid from the user's perspective, but scalable when needed.

replacing testImplementation with implementation did the trick for me.

Kotlin Coroutines in Android — Unit Test | by Evan Fang, There are too many things about that, we need to clarify every class's duty, try to create an interface Before we get started, remember to add the following code to your testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.2" I introduce it here, but not recommend to use it in the real cases. 2 Could not execute method for android:onClick ERROR Oct 8 '19 1 System.out.println(dog1.getName()); returning null Oct 8 '19 1 Android SoundPool is not playing wav format sound Oct 9 '19

just add :-


Source :- Here yigit have given solution link and ssems like working .

Unit Testing Coroutine Suspend Functions using , We are going to use the kotlinx-coroutines-test library, so we'll need to add that to @ExperimentalCoroutinesApi class HeavyWorkerTest { @get:Rule var Maybe it's a bug in the current implementation of runBlockingTest . Final year CS student. Progressing in learning and developing apps in Android using Jetpack and Kotlin and in Web using MERN stack. Exploring other techs on Sundays.

Try adding core as a dependency on your test. It solved the problem for me.


java.lang.NoSuchMethodError: kotlinx.coroutines.SupervisorKt , Trying to use runBlockingTest for my unit tests, and getting the above-mentioned error. Also getting the same error when trying to use TestCoroutineDispatcher. class UserRepositoryImpl( private val remoteUserDao:� Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

How to unit test code with Coroutines, CoroutineContextProvider class is providing proper Dispatcher and we will use it to "org.jetbrains.kotlinx:kotlinx-coroutines-test:$versions. Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more

Eliminating Coroutine leaks in tests, Now, let's consider a simple JUnit 4 test class which may be testing that People make mistakes. @get:Rule val scope = CoroutineTestRule() @Test with the new testing tools introduced in kotlinx.coroutines-test v1.2.1. Q&A for Ubuntu users and developers. Stack Exchange Network. Stack Exchange network consists of 177 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Error calling Dispatchers.setMain() in unit test, Have started to try to use kotlinx-coroutines-test in JUnit unit test but getting following error when i call Dispatchers.setMain() java.lang.

  • What kind of error do you get?
  • Unresolved reference: TypeCoroutineDispatcher
  • Nevermind the bounty, I imported the dependency for the wrong module :-/
  • Why did you need experimental = true? Seems like that is not related to Coroutines.
  • I updated my library version but it still didn't work
  • Please check that you use it in unit tests, not instrumented test @ARYAN