Understanding the Mockito architecture
Mockito applies the proxy design pattern to create mock objects. For concrete classes, Mockito internally uses CGLib to create proxy stubs. CGLib is used to generate dynamic proxy objects and intercept field access. The following URL talks about CGLib:
https://github.com/cglib/cglib
The following sequence diagram depicts the call sequence. The ClassImposterizer
class is a singleton class. This class has a createProxyClass
method for generating a source using CGLib. Finally, it uses reflection to create an instance of the proxy class. Method calls are stubbed using the callback API of MethodInterceptor
.
The MethodInterceptor
class acts as a Java reflection class, java.lang.reflect.InvocationHandler
. Any method call on a mock object (proxy) is handled by a MethodInterceptor
instance.
We'll create a custom mocking framework to handle external dependencies. We'll use the Java reflection framework's dynamic proxy object-creation API. The java...