Argument capture - by value or by reference?

Skip to first unread message

Fr Jeremy Krieg (Home)

Jul 23, 2021, 3:57:49 AM7/23/21
to mockito
Dear Mockito lovers,

I pray that you are all well.

I had a quick question: the model in Mockito clearly is that you capture the arguments, and then verify them.

I was using Mockito in a dynamic environment, and the question occurred to me: when Mockito captures the argument, does it store a reference to the argument, or does it somehow make a copy?

This is relevant because in a dynamic environment, the argument obviously might have changed between the time the invocation was captured and the time when you are attempting to run the matcher against it during verification. I'm interested in what the object looked like at the time the invocation was made, not later when I'm running the verification. 

I know that this use case may be a little outside of the anticipated use cases of Mockito, but I thought I'd put the question to see if anyone had come across this issue before and if there was a standard way around it.

Fr Jeremy Krieg


James Hurley

Jul 23, 2021, 7:08:23 PM7/23/21
to mockito
Mockito captures by reference.  I had a related problem several months ago where the argument captures I was performing were changing due to object pooling and this made it impossible to validate them (at least using the stock Mockito classes).

Reply all
Reply to author
0 new messages