hcoles / pitest

State of the art mutation testing system for the JVM
Apache License 2.0
1.67k stars 357 forks source link

Support of Mockk as Mocking Framework for Kotlin #790

Open afaucogney opened 4 years ago

afaucogney commented 4 years ago

I have tried to run a simple test-case with Mockk https://mockk.io/, but it doesn't work.

class PitMockkDepUseCaseTest {

    @Test
    fun test() {
        mockkConstructor(PitProvider::class)
        every { anyConstructed<PitProvider>().provideNumber() } returns 4
        Assertions.assertThat(PitDepUseCase().execute(1)).isEqualTo(5)
    }
}

The same test with Mockito worked.

class PitMockitoDepUseCaseTest {

    @Test
    fun test() {
        mock<PitProvider> {
            on {
                provideNumber()
            } doReturn (4)
        }
        Assertions.assertThat(PitDepUseCase().execute(1)).isEqualTo(5)
    }
}

I get a sequence of exception when I run the gradle tasks.

Caused by: java.lang.ClassNotFoundException: io.mockk.proxy.MockKAgentFactory

Is there a way to support Mockk ?

afaucogney commented 4 years ago

After debugging, I added the following dependencies to make it running :

    testImplementation "io.mockk:mockk-agent-api:1.10.0"
    testImplementation "io.mockk:mockk-agent-jvm:1.10.0"
    testImplementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

But I think this should be easy as it is with Mockito and others.