Mockk matching and overloaded function withArg

2.1k views Asked by At

Hello I am trying to find a way to match an overloaded function inside of the verify using withArg

The doc doesnt really point this out

    every { getResponse.Ids } returns listOf(121212L)
    assert( client.getExtIds(Ids) )
    verify {
        client.getExtIdsCall().call(
            withArg {
                assertEquals(GetExtIdsRequest.builder()
                    .withIds("foo")
                    .withType("bar")
                    .build().hashCode(), it.hashCode()
                )
            }
        )
    }

Something like above. But unfortunately I cant because the client.getExtIdsCall().call() accepts two different types of objects. One of which has the hashCode I want. So the it can not be referred correctly to call the hashCode function

2

There are 2 answers

1
Karsten Gabriel On BEST ANSWER

You can resolve this by explicitly specifying the type parameter of function withArg, e.g. if you want your parameter to be a Long, you can write:

withArg<Long> { ... }
0
Luis Possatti On

Based on this issue in the Mocck github page, seems like the recommended approach is to use match instead of withArgs to make this verification.