As it seems, Mockk mocks only store the reference to the received arguments for later verification. This poses a problem as soon as the argument becomes modified throughout the test. How could one verify a mock function invocation for deep equality of the arguments?
The following code snippet demonstrates the problem:
data class Entity(var status: String)
interface Processor {
fun process(entity: Entity)
}
class ProcessorTest {
@Test
fun `should receive untouched entity`() {
val myEntity = Entity("untouched")
val processorMock: Processor = mockk()
every { processorMock.process(any()) }.answers {
println(firstArg<Entity>().status)
}
processorMock.process(myEntity) // process untouched entity
myEntity.status = "touched" // afterwards it becomes touched
verify { processorMock.process(Entity("untouched")) }
}
}
processorMock::process
is expectedly and apparently being called with an "untouched" entity, as it prints "untouched". However the verification fails with:
java.lang.AssertionError: Verification failed: call 1 of 1: Processor(#1).process(eq(Entity(status=untouched)))). Only one matching call to Processor(#1)/process(Entity) happened, but arguments are not matching:
[0]: argument: Entity(status=touched), matcher: eq(Entity(status=untouched)), result: -
I have tried using a CapturingSlot
with no success, either.
Mockk version is 1.9.3, Kotlin version is 1.3.70. I am using JUnit 5 as a testing framework.
Any help will be much appreciated!