mockito如何创建模拟对象的实例 [英] How does mockito create an instance of the mock object

查看:165
本文介绍了mockito如何创建模拟对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建一个类Employee的模拟对象。它不调用Employee对象的构造函数。我知道内部Mockito使用CGLIb和反射,创建一个代理类,将类扩展到mock。如果它不调用employee的构造函数,那么如何创建employee类的mock实例?

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使用CGLib生成类对象。但是,要实例化此类对象,它使用Objenesis http://objenesis.org/tutorial.html

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

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

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

这篇关于mockito如何创建模拟对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆