2017-03-01 14 views
5

फेंकता है मैं एक मॉक ऑब्जेक्ट बनाने के लिए मॉकिटो का उपयोग करने की कोशिश कर रहा हूं जो एक मॉक ऑब्जेक्ट से वापस आ जाता है। विशेष रूप से, मैं PlayerConnection ऑब्जेक्ट का नकल करने की कोशिश कर रहा हूं कि मेरा प्रोग्राम आईपी पता पुनर्प्राप्त करने के लिए उपयोग कर सकता है।मॉकिटो जब()। फिर रीटर्न() nullpointerExceptions

आप इस PlayerConnection objecthere के बारे में अधिक जानकारी प्राप्त कर सकते हैं। यह InetSocketAddress देता है जो उसके बाद InetAddress लौटा सकता है जो खिलाड़ी के आईपी के साथ String वापस कर सकता है। लेकिन मैं इसे दूर नहीं कर सकता, क्योंकि मेरा पहला when(class.function()).thenReturn(returnVariable)NullPointerException फेंकता है। यहाँ मेरी कोड है:

/** 
* Creates a partial mock of a connection that can return an ip address. 
* 
* @param String 
*   The IP to return when the connection gets asked. 
* @return 
*/ 
private PlayerConnection newConnection(String ipString) 
{ 
    PlayerConnection playerConnection = mock(PlayerConnection.class); 
    InetSocketAddress inetSocketAddress = mock(InetSocketAddress.class); 
    InetAddress inetAddress = mock(InetAddress.class); 

    when(playerConnection.getAddress()).thenReturn(inetSocketAddress); 
    when(inetSocketAddress.getAddress()).thenReturn(inetAddress); 
    when(inetAddress.getHostAddress()).thenReturn(ipString); 

    return playerConnection; 
} 

और यहाँ स्टैक ट्रेस है, when(playerConnection.getAddress()).thenReturn(inetSocketAddress) में उत्पन्न:

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.001 sec <<< FAILURE! 
ruleResponseTest(com.github.heartsemma.communitywall.ConnectionChecks.RuleManagerTest) Time elapsed: 0.001 sec <<< ERROR! 
java.lang.NullPointerException 
     at java.net.InetSocketAddress$InetSocketAddressHolder.access$500(InetSocketAddress.java:56) 
     at java.net.InetSocketAddress.getAddress(InetSocketAddress.java:334) 
     at com.github.heartsemma.communitywall.ConnectionChecks.RuleManagerTest.newConnection(RuleManagerTest.java:99) 
     at com.github.heartsemma.communitywall.ConnectionChecks.RuleManagerTest.ruleResponseTest(RuleManagerTest.java:44) 

संपादित करें:

मैं when().thenReturn() के बजाय doReturn().when().function() करने के लिए अपने स्टब्स में बदला गया है NullPointerExceptions को रोकने के लिए, और ऐसा हुआ, लेकिन अब मुझे मॉकिटो से कस्टम UnfinishedStubbingExceptions मिल रहा है।

उपयोगी त्रुटि कोड कहता है कि मेरे पास कहीं अधूरा स्टब है, लेकिन मुझे नहीं पता कि यह कहां है। त्रुटि दूसरी doReturn() विधि पर होती है।

/** 
* Creates a partial mock of a connection that can return an ip address. 
* 
* @param ipString The IP to return. 
*    
* @return A PlayerConnection object that can return a Host Address of the ipString but nothing else. 
*/ 
private PlayerConnection newConnection(String ipString) 
{ 
    PlayerConnection playerConnection = mock(PlayerConnection.class); 
    InetSocketAddress inetSocketAddress = mock(InetSocketAddress.class); 
    InetAddress inetAddress = mock(InetAddress.class); 

    doReturn(inetSocketAddress).when(playerConnection).getAddress(); 
    doReturn(inetAddress).when(inetSocketAddress).getAddress(); 
    doReturn(ipString).when(inetAddress).getHostAddress(); 

    return playerConnection; 
} 
+0

http: // stackoverflow देखें।com/प्रश्न/34308877/कैसे करने वाली नकली विधि-कॉल-एंड-वापसी-मान-बिना चलने-विधि। – Tunaki

+0

मुझे समझ में नहीं आता है। आपके पास एक ही त्रुटि है, और वही जवाब 'doReturn (inetSocketAddress) का उपयोग करना है। जब (प्लेयरकनेक्शन) .getAddress() 'जैसा कि उल्लेख किया गया है, और आप ऐसा नहीं कर रहे हैं (तो आप कैसे कह सकते हैं कि इससे मदद नहीं मिली?)। – Tunaki

+0

क्षमा करें; मेरा कहना था कि यह एक और त्रुटि हुई। मूल पोस्ट संपादन। –

उत्तर

7

सारांश:InetSocketAddress.getAddress(), अंतिम है as listed in the docs। इसके चालाक वाक्यविन्यास के कारण, मॉकिटो आसानी से final विधियों को साफ़ या सत्यापित नहीं कर सकता है, और यह तब भी नहीं बता सकता जब यह कोशिश कर रहा है और विफल रहा है। आम तौर पर, उन वस्तुओं को नकल न करें जिन्हें आप नियंत्रित नहीं करते हैं, खासतौर पर इस तरह की परिस्थितियों के कारण।

UnfinishedStubbingException के लिए उपयोगी त्रुटि कोड को दिखाता है आपकी समस्या (, आप शरारती डेवलपर को देखने के विकल्प # 2!):

org.mockito.exceptions.misusing.UnfinishedStubbingException: 
Unfinished stubbing detected here: 
-> at 

E.g. thenReturn() may be missing. 
Examples of correct stubbing: 
    when(mock.isOk()).thenReturn(true); 
    when(mock.isOk()).thenThrow(exception); 
    doThrow(exception).when(mock).someVoidMethod(); 
Hints: 
1. missing thenReturn() 
2. you are trying to stub a final method, you naughty developer! 
3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed 

विवरण: परम्परागत Mockito एक वस्तु लेने और स्वचालित रूप से से काम मजाक उड़ाता है और चुपचाप एक सबक्लास उत्पन्न करना जहां मॉकिटो को प्रतिनिधि के लिए सभी विधियों को ओवरराइड किया गया है। हालांकि, जावा कंपाइलर अंतिम तरीकों के लिए स्थैतिक प्रेषण का लाभ उठाता है, इसलिए अंतिम विधियों पर कॉल सीधे मॉकिटो पर नहीं जाते हैं, वे इसके बजाय InetSocketAddress में वास्तविक विधि कार्यान्वयन पर जाते हैं। इस सबक्लास उदाहरण का कभी भी बातचीत करने का इरादा नहीं है, और इसमें से कोई भी फ़ील्ड सेट नहीं है, इसलिए NullPointerException या अन्य व्यवहार प्राप्त करना बहुत आसान है- और यह सब Mockito से बातचीत करने से पहले होता है, इसलिए आप ' when(object.methodCall()).thenReturn() का उपयोग करते समय भी एक सैक त्रुटि संदेश का लाभ प्राप्त नहीं होता है, क्योंकि Mockito में शामिल होने से पहले object.methodCall() का मूल्यांकन करते समय अप्रत्याशित एनपीई होता है।

काम करना अन्य रास्ता doReturn(...).when(object).methodCall() -gives doReturn और when देखने के लिए भले ही methodCall करने के लिए कॉल Mockito प्रतिनिधि नहीं है मौका Mockito। यह मॉकिटो को यह कहने का संदर्भ देता है कि स्टबिंग अधूरा है, क्योंकि मॉकिटो methodCall कॉल नहीं देख सकता है।

अधिक जानकारी के लिए, यह SO प्रश्न (Final method mocking) देखें और हाल ही में opt-in mocking of final classes and methods का उपयोग करने पर विचार करें जो Mockito संस्करण 2.1 में पेश किया गया है।

+0

बहुत बहुत धन्यवाद। मुझे नहीं पता था कि InetSocketAddress.getAddress() अंतिम था। –

+0

अच्छा सवाल, अच्छा जवाब! – GhostCat