Skip to content
Projetos

Sobre

A mocking processor for Kotlin Multiplatform

Outros projetos

Como estes números são medidos

Crescimento medido de estrelas

em 24 h
0
em 7 dias
0
em 15 dias
0
em 1 mês
Coletando histórico

Variações líquidas em períodos sobrepostos, não totais diários. Medições ausentes ficam em branco. Estrelas indicam interesse, não qualidade.

README.md

Original

MocKMP

Compile-time mocks and fakes for Kotlin Multiplatform tests — generated by a KSP processor, not by reflection.

MocKMP turns a plain interface or abstract class into a mock or a fake for your tests. You ask for what a test needs with an annotation — @Mock, @Fake, @UsesMocks, @UsesFakes — and a KSP processor generates the implementation, ready to inject.

MyTest.kt
class MyTest : TestsWithMocks() {
    override fun setUpMocks() = mocker.injectMocks(this)

    @Mock lateinit var view: View
    @Fake lateinit var model: Model

    val controller by withMocks { Controller(view = view, firstModel = model) }

    @Test fun controllerTest() {
        every { view.render(isAny()) } returns true
        controller.start()
        verify { view.render(model) }
    }
}

No reflection, no dynamic proxies, no "relaxed" mode papering over a call nobody mocked. An interface, a generated implementation, and a compiler that rejects a mock or a fake for a type that can’t be one.

Why MocKMP

Compile-time, not reflection. A KSP processor generates a real class implementing the interface or abstract class you asked to mock or fake, so it behaves identically on every target — including the ones with no runtime reflection at all, like iOS, JS and Wasm.

Mocking and faking answer different questions. A mock’s behaviour is defined per test, and every call on it can be verified; a fake’s data is inert and zero-valued, and nothing about it is checked. Reach for a fake when a collaborator only needs to exist, and a mock when the test needs to configure or verify it.

Nothing is generated until you ask for it. @UsesMocks / @UsesFakes — or a @Mock / @Fake property — requests exactly the types a given test needs, so an interface used across a large codebase doesn’t grow generated code everywhere it’s declared, only where it’s actually mocked or faked.

Injection kills the constructor boilerplate. Annotate properties @Mock or @Fake and call mocker.injectMocks(this); inherited properties are injected too, so a shared base test class works exactly as if its mocks had been declared locally.

The TestsWithMocks helper collapses the ceremony further. Inherit it and get a Mocker, an automatic reset before every test, withMocks property delegates rebuilt fresh each time, and every / everySuspending / verify / verifyWithSuspend callable without the mocker. prefix.

Constraints read the same in a definition and in a verification. isAny, isEqual, isNotNull, isInstanceOf, and any custom constraint you write with isValid, work identically whether they appear inside every or inside verify — one matcher vocabulary, not two.

Multiplatform, with no platform-specific surprises. mockmp-runtime targets every platform MocKMP supports and behaves the same on each of them.

Installation

Warning
Version 3.0 brings compatibility with AGP 9, but becomes incompatible with AGP 8 or earlier. Only update to MocKMP 3.0 if you are also updating to AGP 9 — if you’re on version 2.1, see the 2.x to 3.x migration guide.

Apply the Gradle plugin to your Kotlin/Multiplatform module, then enable it on the test source sets you want mocks and fakes generated for:

build.gradle.kts
plugins {
    kotlin("multiplatform")
    id("com.google.devtools.ksp")                    // (1)
    id("org.kodein.mock.mockmp") version "3.5.5" // (2)
}

mockmp {                                             // (3)
    onTest {
        withHelper()
    }
}
  1. KSP hosts the MocKMP processor, and its version must match your Kotlin version.

  2. The MocKMP Gradle plugin wires the runtime and the processor for you, so there is no dependency to add by hand.

  3. Configured after the kotlin { } block, so the plugin can see your targets. withHelper() also pulls in the TestsWithMocks helper class.

For the other platforms and setups, see the setup page.

Documentation

The full documentation is on kosi-libs.org.

Explore projetos relacionados

Carregando…