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.

Understanding the Mockito architecture

The

Get Mockito Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.