给定以下代码:
LinkedList list = mock(LinkedList.class);
doCallRealMethod().when(list).clear();
list.clear();
通过执行此测试,从 LinkedList#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 源代码,发现它使用反射 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;
}
}