Open22

Kotlin で モックテスト

tkttkt

導入

(app)
dependencies {
    testImplementation 'io.mockk:mockk:1.11.0'
}

これ書く
sync gradle する

tkttkt

テストに行って、
mockk って書いて呼び出そうとしてもインポートも何もしてくれなくてあたふた

tkttkt

自分で

import io.mockk.*

書いたら読んでくれたので、このまま書いていく

tkttkt
Unresolved reference: io
Unresolved reference: io
Unresolved reference: mockk
Unresolved reference: every
tkttkt
androidTestImplementation "io.mockk:mockk-android:{version}"

これ書き足したら通った

tkttkt

テストはなんか mock 部分がぬるぽでおちた

tkttkt

コンストラクタが引数ありの場合は

val client = spyk<(context: Context) -> Client>()

こんな感じで書けるかも

tkttkt
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.mockk.confirmVerified
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class MockkTest {
    @Test
    fun mock_test() {
        val base = mockk<Base>()
        every { base.getHoge() } returns "mocked"

        val result = base.getHoge()
        Assert.assertEquals("mocked", result)

        verify {
            base.getHoge()
        }
        confirmVerified(base)
    }

    class Base {
        fun getHoge(): String {
            return "moge"
        }
    }
}
io.mockk.MockKException: Missing mocked calls inside every { ... } block: make sure the object inside the block is a mock
at io.mockk.impl.recording.states.StubbingState.checkMissingCalls(StubbingState.kt:14)
at io.mockk.impl.recording.states.StubbingState.recordingDone(StubbingState.kt:8)
at io.mockk.impl.recording.CommonCallRecorder.done(CommonCallRecorder.kt:47)
at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:64)
at io.mockk.impl.eval.EveryBlockEvaluator.every(EveryBlockEvaluator.kt:30)
at io.mockk.MockKDsl.internalEvery(API.kt:92)
at io.mockk.MockKKt.every(MockK.kt:98)
tkttkt
Unresolved reference: mockito
Unresolved reference: mockito
Unresolved reference: mock
Unresolved reference: on
Unresolved reference: getHoge

mockio でも出た…
AndroidStudio 調子悪い?

tkttkt

mockk でいじってたら java.util.Collections$SingletonList cannot be cast to みたいなエラーに変わった

tkttkt

mockito に関しては読み込みすらできない

tkttkt

https://stackoverflow.com/questions/53391611/mockk-missing-calls-inside-every-block

class MockkTest {
    @Test
    fun mock_test() {
        val base = spyk(Base())
        every { base.getHoge() } returns "mocked"

        val result = base.getHoge()
        Assert.assertEquals("mocked", result)

        verify {
            base.getHoge()
        }
        confirmVerified(base)
    }

    open class Base {
        fun getHoge(): String {
            return "moge"
        }
    }
}
io.mockk.MockKException: Missing mocked calls inside every { ... } block: make sure the object inside the block is a mock
at io.mockk.impl.recording.states.StubbingState.checkMissingCalls(StubbingState.kt:14)
at io.mockk.impl.recording.states.StubbingState.recordingDone(StubbingState.kt:8)
at io.mockk.impl.recording.CommonCallRecorder.done(CommonCallRecorder.kt:47)
at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:60)
at io.mockk.impl.eval.EveryBlockEvaluator.every(EveryBlockEvaluator.kt:30)
at io.mockk.MockKDsl.internalEvery(API.kt:92)
at io.mockk.MockKKt.every(MockK.kt:98)