1

I'm trying to use "runTest()" in Kotlin multiplatform. I'm using Jetbrains's "Getting started"-project as an example. (https://kotlinlang.org/docs/multiplatform-library.html)

The problem is that runTest() does not find a coroutine context. It gives me the following build error:

Cannot access class 'kotlin.coroutines.CoroutineContext'. Check your module classpath for missing or conflicting dependencies

Here is my test:

class Base64JvmTest {
@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun testNonAsciiString() {
  runTest {
        val utf8String = "Gödel"
        val actual = Base64Factory.createEncoder().encodeToString(utf8String.toByteArray())
        assertEquals("R8O2ZGVs", actual)
    }
  }
}

In build.gradle.kts, I set the following in kotlin.sourceSets:

val jvmTest by getting {
  dependencies {
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4")
  }
}

Please help me out - what am I missing?

1
  • A bit further... It turns out that ./gradlew test works, so there is something wrong in Idea. Clearing the cache usually fixes this kind of errors, but not this time. Commented Aug 3, 2022 at 12:53

2 Answers 2

2

As it turns out, there was an issue with Idea. I added the following dependency to get rid of the error:

dependencies {
  commonTestImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4")
}

It shouldn't really be needed, as the common tests are not dependent on coroutines, but an acceptable work-around.

Sign up to request clarification or add additional context in comments.

Comments

0

you can run runTest following way as documentation is suggested

@Test
fun exampleTest() = runTest {
    val deferred = async {
        delay(1_000)
        async {
            delay(1_000)
        }.await()
    }

    deferred.await() // result available immediately
}

documentation code link

3 Comments

Thank you for your answer. But the issue is with runTest(), so your code has the same problem.
@good you have missed to add dependency in gradle I glad you have fixed problem
The dependency I added shouldn't be needed, but Idea won't build without it. Intellij has done a great job with Idea, but there are a few bugs like this that wastes quite a lot of time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.