Mock a boundary, not the calculation under test
MockK can supply controlled behavior for a dependency and verify important interactions. Add a compatible MockK JVM test dependency in the learning project; keep the production code independent from the mocking library.
import io.mockk.*
import kotlin.test.*
interface Titles { fun find(id: String): String? }
class Labels(private val titles: Titles) {
fun label(id: String) = titles.find(id) ?: "Unknown"
}
class LabelsTest {
@Test fun usesFoundTitle() {
val titles = mockk<Titles>()
every { titles.find("k1") } returns "Kotlin"
assertEquals("Kotlin", Labels(titles).label("k1"))
verify(exactly = 1) { titles.find("k1") }
}
}
Use coEvery and coVerify for suspending members. Avoid verifying every incidental call order; doing so can make harmless refactors break tests. A small fake is often clearer for a stateful repository.
Exercise
Add a missing-title test and compare the mock with a map-backed fake.
Check: the real Labels implementation must run; mocking the class under test would only verify your stubbing.