問題描述
當我創(chuàng)建一個說類 Employee 的模擬對象時.它不調(diào)用 Employee 對象的構(gòu)造函數(shù).我知道內(nèi)部 Mockito 使用 CGLIb 和反射,創(chuàng)建一個代理類,將類擴展為模擬.如果不調(diào)用employee的構(gòu)造函數(shù),employee類的mock實例是如何創(chuàng)建的?
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 能夠使用各種技術(shù)(即調(diào)用 ObjectStream.readObject 和類似方法)在沒有構(gòu)造函數(shù)的情況下實例化對象.
Objenesis is able to instantiate object without constructor using various techniques (i.e. calling ObjectStream.readObject and similar).
這篇關(guān)于mockito如何創(chuàng)建mock對象的實例的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!