鑑於以下代碼:的Mockito - 懲戒具體類
LinkedList list = mock(LinkedList.class);
doCallRealMethod().when(list).clear();
list.clear();
通過執行該試驗中,一個NullPointerException是從鏈表#第一線清晰拋出:
public void clear() {
Entry<E> e = header.next;
while (e != header) {
Entry<E> next = e.next;
//Code omitted.
但頭一直實例化之前:
private transient Entry<E> header = new Entry<E>(null, null, null);
莫非一些請解釋模擬創作過程中發生了什麼?
#######更新。 ######
閱讀所有答案,特別是Ajay的答案後,我查看了Objenesis源代碼,並發現它使用Reflection API創建代理實例(通過CGLIB),因此繞過層次結構中的所有構造函數直到java.lang.Object。
下面是示例代碼模擬了問題:
public class ReflectionConstructorTest {
@Test
public void testAgain() {
try {
//java.lang.Object default constructor
Constructor javaLangObjectConstructor = Object.class
.getConstructor((Class[]) null);
Constructor mungedConstructor = ReflectionFactory
.getReflectionFactory()
.newConstructorForSerialization(CustomClient.class, javaLangObjectConstructor);
mungedConstructor.setAccessible(true);
//Creates new client instance without calling its constructor
//Thus "name" is not initialized.
Object client = mungedConstructor.newInstance((Object[]) null);
//this will print "CustomClient"
System.out.println(client.getClass());
//this will print "CustomClient: null". name is null.
System.out.println(client.toString());
} catch(Exception e) {
e.printStackTrace();
}
}
}
class CustomClient {
private String name;
CustomClient() {
System.out.println(this.getClass().getSimpleName() + " - Constructor");
this.name = "My Name";
}
@Override
public String toString() {
return this.getClass().getSimpleName() + ": " + name;
}
}
我們展示了全堆棧跟蹤 – Archer