kotlinx.coroutines: java.lang.NoSuchMethodError: kotlinx.coroutines.SupervisorKt.SupervisorJob

Trying to use runBlockingTest for my unit tests, and getting the above-mentioned error.

Here are my code snippets:

@Test
    fun `test verify test call`() = runBlockingTest {
        registerViewModel.testCall()
        verify(repo).suspendedTestCall()
    }
fun testCall() {
        viewModelScope.launch(Dispatchers.IO) {
            repository.suspendedTestCall()
        }
    }
suspend fun suspendedTestCall() {
        kotlinx.coroutines.delay(2_000)
    }

Also getting the same error when trying to use TestCoroutineDispatcher.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 2
  • Comments: 17 (6 by maintainers)

Commits related to this issue

Most upvoted comments

Does your version of kotlinx-coroutines-test matches with the version of kotlinx-coroutines-core?

The problem here is not in the library itself but in transitive dependencies.

How to verify you have this problem

In Android Studio or IntelliJ IDEA press Find symbol hotkey (cmd/ctrl+ N) and type kotlinx.coroutines.Job. If you see more than two versions of the class (example), it’s a dependency clash.

How to resolve the problem

You have multiple options here:

  1. Check whether dependencies that rely on coroutines can be updated and update them, if so
  2. Explicitly add kotlinx-coroutines-core (additionally to kotlinx-coroutines-android or kotlinx-coroutines-test or any other kx-coroutines dependency) with the desired version to your dependencies
  3. Resolve to the desired version of kotlinx.coroutines explicitly using custom Gradle resolution strategy

@JakubMosakowski thanks for the reproducer!

Can you, please, check that your kotlinx-coroutines-core and kotlinx-coroutines-test versions match.