且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

mockito 如何创建模拟对象的实例

更新时间:2022-10-23 12:26:49

Mockito 使用 CGLib 生成类对象.但是要实例化此类对象,它使用 Objenesis http://objenesis.org/tutorial.html>

Objenesis 能够使用各种技术(即调用 ObjectStream.readObject 等)在没有构造函数的情况下实例化对象.

When i create a mock object of say class Employee. It doesnt call the constructor of Employee object. I know internally Mockito uses CGLIb and reflection, creates a proxy class that extends the class to mock. If it doesnt call the constructor of employee how is the mock instance of employee class created ?

Mockito uses CGLib to generate class object. However to instantiate this class object it uses Objenesis http://objenesis.org/tutorial.html

Objenesis is able to instantiate object without constructor using various techniques (i.e. calling ObjectStream.readObject and similar).