0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Mockkで引数なしで非同期なプライベートメソッドをスパイ化する時はUnitにキャストしよう

Last updated at Posted at 2025-06-09
Sample.kt
class SampleA() {
    suspend fun methodA() = methodB()
    private suspend fun methodB() = { Log.i("Sample", "ここでいろんな処理をする") }
}
SampleTest.kt
class SampleATest() {
    private sampleA = spyk(SampleA(), recordPrivateCalls = true)

    @Test
    fun test_sample_A() = runTest {
        coEvery { sampleA["methodB"]() as Unit } just Runs // as Unitが大事

        sampleA.methodA()

        verify { sampleA["methodB"]() }
    }
}

as UnitをつけずにIDEの示す通り脳停止で書くと coEvery { sampleA["methodB"]() } just Awaitになり、一生UnitTestが終わらない

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?