Mockito Magic
Pass-by-reference semantics in a pass-by-value language
Aug 10, 2024
The Mockito library shouldn’t work. When I pass an argument to any Java method, the method does not (in fact, cannot) know how that argument was created — was it instantiated in the method call? was it created in a separate class? up the thread stack? is it null? But in Mockito, we can do stuff like Mockito.when(mockInstance.mockMethod()).thenReturn("abcd")
. This shouldn’t work. And yet it does. How does it happen?
I addressed and answered this question in the below YouTube video: