Trouble understanding how a mock is created

44 views
Skip to first unread message

luo weilue

unread,
Feb 11, 2023, 5:05:21 PM2/11/23
to mockito
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:
  1. Mockito find a constructor of the given class (choose the first public if possible, else the first constructor).
  2. 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).
  3. 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.
  4. Create the a MockMethodInterceptor that handles method invocation.
  5. Store instance and method interceptor as key-value into a map called mocks.
  6. ...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
Reply all
Reply to author
Forward
0 new messages