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