निम्नलिखित कोड को देखते हुए मजाक:Mockito ठोस वर्ग
LinkedList list = mock(LinkedList.class);
doCallRealMethod().when(list).clear();
list.clear();
इस परीक्षण को क्रियान्वित करते हुए, एक NullPointerException LinkedList # में पहली पंक्ति स्पष्ट से फेंक दिया जाता है:
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);
कुछ एक कृपया नकली सृजन के दौरान क्या हो रहा है समझाओ?
####### अद्यतन। ######
सभी जवाब विशेष रूप से अजय की एक, मैं Objenesis स्रोत कोड में देखा पढ़ सकते हैं और है कि यह छद्म उदाहरण (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