【发布时间】:2013-01-17 08:07:13
【问题描述】:
给定以下代码:
LinkedList list = mock(LinkedList.class);
doCallRealMethod().when(list).clear();
list.clear();
通过执行这个测试,从 LinkedList 的第一行抛出 NullPointerException#clear:
public void clear() {
Entry<E> e = header.next;
while (e != header) {
Entry<E> next = e.next;
//Code omitted.
但是 header 之前已经实例化过:
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;
}
}
【问题讨论】:
标签: java unit-testing mocking mockito