6

I've noticed that sometimes verify fails with "... call to ... happened, but arguments are not matching"

Here is a sample test that shows verify failing:

class TestStuff {

    val stuff = "1"

    @RelaxedMockK
    lateinit var testService: TestService

    @RelaxedMockK
    lateinit var testInterface: TestInterface

    @Before
    fun setup() {
        MockKAnnotations.init(this)

        every { testInterface.testStuff } returns stuff
    }

    @Test
    fun testStuffCalled() {
        testService.testStuff(testInterface.testStuff)

        verify { testService.testStuff(testInterface.testStuff) }
    }
}

interface TestInterface {
    val testStuff: String
}

class TestService {

    fun testStuff(stuff: String) {
    }
}

If I change the line with the verify call to the following 2 lines, then it works:

let testStuffCopy = testInterface.testStuff
verify { testService.testStuff(testStuffCopy) }
jc12
  • 1,411
  • 1
  • 18
  • 24

2 Answers2

3

I'm unsure if this is a bug, but a quick workaround would be to use stuff as the verification, as you want the returned value to be it:

verify { testService.testStuff(stuff) }

This way you still test that the behaviour was called, and as you mocked the return of testInterface to return stuff, this should work.

I created an Issue in Mockk for this, and I'll update this answer when something is updated there.

LeoColman
  • 6,950
  • 7
  • 34
  • 63
  • Thanks! I'm confused why that works and the other way doesn't since `testInterface.testStuff` should be returning `stuff`, so shouldn't it be the same? – jc12 Feb 21 '18 at 19:17
  • It returns stuffw hen you call it. As you're trying to pass it as a parameter to an already mocked method, I believe it's not calling the stubbed behaviour when you're verifying – LeoColman Feb 22 '18 at 01:02
  • Then again, I'm unsure if this is actually a bug or just a countereffect – LeoColman Feb 22 '18 at 01:04
  • I freaked out with this. You've saved me from having to throw away the test, thanks. – Rubén Viguera Jan 24 '23 at 22:39
0

In "verify", try to use "any()" instead of specific parameters to get rid of this issue.

Hassan
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 20 '23 at 14:24