問題描述
當我創建一個說類 Employee 的模擬對象時.它不調用 Employee 對象的構造函數.我知道內部 Mockito 使用 CGLIb 和反射,創建一個代理類,將類擴展為模擬.如果不調用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如何創建mock對象的實例的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!