5

I have a very simple class:

class TestClass {

    companion object {
        fun sampleFunc(value: Int): Int {
            return value + 5
        }
    }
}

and a very simple test:

@Test
fun `test class`() {
    mockkObject(TestClass::class)
    every {
        TestClass.sampleFunc(any())
    } returns 11

    assertThat(TestClass.sampleFunc(5)).isEqualTo(11)
}

Stack trace:

Failed matching mocking signature for

left matchers: [any()]
io.mockk.MockKException: Failed matching mocking signature for

left matchers: [any()]
    at io.mockk.impl.recording.SignatureMatcherDetector.detect(SignatureMatcherDetector.kt:99)
    at io.mockk.impl.recording.states.RecordingState.signMatchers(RecordingState.kt:39)
    at io.mockk.impl.recording.states.RecordingState.round(RecordingState.kt:31)
    at io.mockk.impl.recording.CommonCallRecorder.round(CommonCallRecorder.kt:50)
    at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:59)
    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:104)

MockK version: 1.9.3 OS: mac Kotlin version: 1.5.21 JDK version: 11 JUnit version: 4.12

Any idea? I also tried mockkStatic with the same results.

Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106
  • I am not quite sure but shouldn't you have used a regular class mocking here like `val testClass = mockkClass(TestClass::class)` and then `every { testClass.something(any()) } returns 11` – nuhkoca Oct 15 '21 at 21:47
  • Actually I tried that @nuhkoca and it would not compile. It is a static class. – Kristy Welsh Oct 16 '21 at 18:04

1 Answers1

13

According to this answer: https://github.com/mockk/mockk/issues/136#issuecomment-419879755

@Test
fun `test class`() {
    mockkObject(TestClass.Companion)
    every {
        TestClass.sampleFunc(any())
    } returns 11

    assertThat(TestClass.sampleFunc(5)).isEqualTo(11)
}

Does the trick, and it does.

Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106