Hi, I am trying to get my first Kotlin unit test running - using MockK. Running the test results in ...
m

Mario Adam

over 2 years ago
Hi, I am trying to get my first Kotlin unit test running - using MockK. Running the test results in failure. This is my class to be tested:
class Client(private val context: Context) {
    fun sendCommandString(command: String, parameter: String) {
        val intent = Intent()
        intent.action = Intents.ACTION_DATAWEDGE
        intent.putExtra(command, parameter)
        intent.putExtra(Intents.EXTRA_SEND_RESULT, "true")
        context.sendBroadcast(intent)
    }
}
and this is my test:
class ClientTest : MockkUnitTest() {
    @RelaxedMockK
    lateinit var context: Context

    @Test
    fun verifySendCommandString() = runTest {
        // Arrange
        every { context.sendBroadcast(any()) } returns Unit
        val client = Client(context)

        // Act
        client.sendCommandString("some command", "some parameter")

        // Assert
        verify(exactly = 1) {
            context.sendBroadcast(withArg {
                Truth.assertThat(it.action).isEqualTo(Intents.ACTION_DATAWEDGE)
                Truth.assertThat(it.extras).isNotNull()
                Truth.assertThat(it.extras!!.containsKey("some command")).isTrue()
                Truth.assertThat(it.getStringExtra("some command")).isEqualTo("some parameter")
                Truth.assertThat(it.extras!!.containsKey(Intents.EXTRA_SEND_RESULT)).isTrue()
                Truth.assertThat(it.getStringExtra(Intents.EXTRA_SEND_RESULT)).isEqualTo("true")
            })
        }
    }
}
The error is:
Method setAction in android.content.Intent not mocked.
. Well - I know. There should be no need to mock this method. I only have mocked the context to ensure, a correct
intent
has been created. (also posted on StackOverflow: https://stackoverflow.com/questions/76287781/test-complains-about-unmocked-method)
We’re running into a strange issue in release iOS builds with the new memory model. Doesn’t happen i...
a

ankushg

over 3 years ago
We’re running into a strange issue in release iOS builds with the new memory model. Doesn’t happen in debug builds. When a user opens/closes a particular screen multiple times, on the ~12th try, we trigger the GC, and it crashes the app. The stracktrace looks something like:
Crashed: GC thread
0  QuizletSharedKotlin            0x919294 std::__1::invoke_result<kotlin::gc::ConcurrentMarkAndSweep::ConcurrentMarkAndSweep(kotlin::mm::ObjectFactory<kotlin::gc::ConcurrentMarkAndSweep>&, kotlin::gc::GCScheduler&)::$_2>::type kotlin::ScopedThread::Run<kotlin::gc::ConcurrentMarkAndSweep::ConcurrentMarkAndSweep(kotlin::mm::ObjectFactory<kotlin::gc::ConcurrentMarkAndSweep>&, kotlin::gc::GCScheduler&)::$_2>(kotlin::ScopedThread::attributes, kotlin::gc::ConcurrentMarkAndSweep::ConcurrentMarkAndSweep(kotlin::mm::ObjectFactory<kotlin::gc::ConcurrentMarkAndSweep>&, kotlin::gc::GCScheduler&)::$_2&&) + 1292
1  QuizletSharedKotlin            0x91a214 void* std::__1::__thread_proxy<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct> >, void (*)(kotlin::ScopedThread::attributes, kotlin::gc::ConcurrentMarkAndSweep::ConcurrentMarkAndSweep(kotlin::mm::ObjectFactory<kotlin::gc::ConcurrentMarkAndSweep>&, kotlin::gc::GCScheduler&)::$_2&&), kotlin::ScopedThread::attributes, kotlin::gc::ConcurrentMarkAndSweep::ConcurrentMarkAndSweep(kotlin::mm::ObjectFactory<kotlin::gc::ConcurrentMarkAndSweep>&, kotlin::gc::GCScheduler&)::$_2> >(void*) + 112
2  libsystem_pthread.dylib        0x19ac _pthread_start + 148
3  libsystem_pthread.dylib        0xe68 thread_start + 8
It’s super hard for us to create a minimal reproduction of this, but in case it’s relevant, we’ve been able to repro this with: • Kotlin 1.7.10 and 1.7.20-Beta • SqlDelight 1.5.3 and 2.0.0-alpha03 • kotlinx.coroutines 1.6.4 (without the
-native-mt
suffix) • KMP-NativeCoroutines 0.12.6-new-mm Any tips on how to narrow this down?