Hi people,
I am fairly new to mockito and I am trying understand how it works internally. By doing some navigations in ide, my current understanding of what happen when we call mock(someClass.class) is:
- Mockito find a constructor of the given class (choose the first public if possible, else the first constructor).
- Gather arguments for constructor: if arguments are not provided, use default values by argument types (e.g. false for boolean, 0 for int, null for object).
- Use a MemberAccess class to create the new instance.
- if class file version of VM is JAVA_V9 or above, this class is InstrumentationMemberAccessor.
- I dont really understand this class.. create instance by load some custom .class bytecode that has some custom method handle (?)
- else it is ReflectionMemberAccessor.
- Return instance by invoking the given constructor with given argument.
- Create the a MockMethodInterceptor that handles method invocation.
- Store instance and method interceptor as key-value into a map called mocks.
- ...Losing track, what happen next ? When we call mock.someMethod(), how it pass the invocation to the method interceptor?
If someone can give me some pointers it would be really helpful, thanks!
Best Regards,
Weilue