Given the following Mockito statement:
when(mock.method()).thenReturn(someValue);
How does Mockito go about creating a proxying something f
The short answer is, behind the scenes, Mockito uses some kind of global variables/storage to save information of method stub building steps(invocation of method(), when(), thenReturn() in your example), so that eventually it can build up a map on what should be returned when what is called on what param.
I found this article very helpful: Explanation how proxy based Mock Frameworks work (http://blog.rseiler.at/2014/06/explanation-how-proxy-based-mock.html). The author implemented a demonstration Mocking framework, which I found a very good resource for people who want to figure out how these Mocking frameworks work.
In my opinion, it's a typical usage of Anti-Pattern. Normally we should avoid 'side effect' when we implement a method, meaning the method should accept input and do some calculation and return the result - nothing else changed besides that. But Mockito just violates that rule on purpose. Its methods store a bunch of info besides returning the result: Mockito.anyString(), mockInstance.method(), when(), thenReturn, they all have special 'side effect'. That's also why the framework looks like a magic at the first glance - we usually don't write code like that. However, in the mocking framework case, this anti-pattern design is a great design, since it leads to very simple API.