Ayla
12/11/2023, 2:49 PM//commonMian
expect fun isOnMainThread():Boolean
//androidMain
actual fun isOnMainThread()=Looper.getMainLooper().isCurrentThread
when I wrote a test in commonTest
and test in android:
@kotlin.test.Test
fun test(){
//...do some things
assertTrue(isOnMainThread())
}
It reported the following error:`Method getMainLooper in android.os.Looper not mocked.`
I know how to run tests using android local tests with mocking frameworks, or simply using Instrumented tests, but how should I integrate them with the KMP project?ephemient
12/11/2023, 3:23 PMephemient
12/11/2023, 3:23 PMAndrii Yanechko
12/11/2023, 4:19 PMandroidUnitTest
folder and use RobolectricJacob Ras
12/11/2023, 5:48 PMJan Holešovský
12/11/2023, 7:21 PMcoachroebuck
12/11/2023, 10:05 PMcoachroebuck
12/11/2023, 10:07 PMimport android.os.Looper
import io.mockk.every
import io.mockk.mockkStatic
// ...
// Mocking Looper.getMainLooper() using MockK
mockkStatic(Looper::class)
// Provide a mock Looper instance for Looper.getMainLooper()
val mockMainLooper = mockk<Looper>()
every { Looper.getMainLooper() } returns mockMainLooper
// Now you can use mockMainLooper as a mocked instance of Looper.getMainLooper()
// Remember to clear the mocks after your test
after { unmockkAll() }