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.
CodePudding user response:
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.