Search code examples
mockitocglibdynamic-proxy

How does Mockito mock interfaces?


I thought Mockito mocked interfaces through the use of dynamic proxies.

But then I noticed the type of a Mockito-mocked intefaces whilst debugging:

MyInterface$$EnhancerByMockitoWithCGLIB$$9654c88

indicating that CGLIB is used instead of a dynamic proxy.

Can someone please:

  • Clarify my interrogation
  • Point me to the relevant source code location in Mockito

Solution

  • Mockito allows for interchangeable implementations of MockMaker, but by default the implementation is the CGLib-based CglibMockMaker.

    There are a few discussions online ("The Power of Proxies in Java" or "What is the difference between JDK dynamic proxy and CGLib?") about the differences between CGLib and standard Proxy objects; when mocking interfaces it seems that Proxy would be perfectly fine, but using CGLib gives you access to mocking actual classes with implementations, and committing to CGLib even beyond when strictly necessary likely makes the code much easier to follow.