पर एकाधिक कस्टम मैचर्स असाइन करता हूं, मैं एक विधि के लिए दो कस्टम मैचर्स का उपयोग करना चाहता हूं। असल में, अगर मैं विधि VALUE_A पास करता हूं, तो मैं इसे RESULT_A वापस लौटना चाहता हूं, और यदि मैं इसे VALUE_B पास करता हूं, तो मैं इसे RESULT_B वापस लौटना चाहता हूं। तो यहाँ एक कोड अंश है:मॉकिटो अजीब तरीके से कार्य करता है जब मैं एक ही विधि
class IsNonEmpty extends ArgumentMatcher<Get> {
public boolean matches(Object get) {
//For some reason, this method is called when I assign the IsEmpty matcher to MockHtable.get()
//When this happens, the value of the get argument is null, so this method throws an NPE
return Arrays.equals(((Get) get).getRow(), SERIALIZATION_HELPER.getValidBytes(key));
}
}
class IsEmpty extends ArgumentMatcher<Get> {
public boolean matches(Object get) {
return !(Arrays.equals(((Get) get).getRow(), SERIALIZATION_HELPER.getValidBytes(key)));
}
}
[...]
//This line executes just fine
Mockito.when(mockHTable.get(Mockito.argThat(new IsNonEmpty()))).thenReturn(dbResult);
[...]
//This line calls IsNonEmpty.matches() for some reason. IsNonEmpty.matches() throws an NPE
Mockito.when(mockHTable.get(Mockito.argThat(new IsEmpty()))).thenReturn(emptyResult);
जब मैं mockHTable.get() विधि को IsEmpty कस्टम मिलान असाइन करते हैं, यह IsNonEmpty.matches() फ़ंक्शन को कॉल। कोई विचार नहीं कि यह क्यों कर रहा है। तो मैं इस पर IsNonEmpty क्लास को बदलता हूं:
class IsNonEmpty extends ArgumentMatcher<Get> {
public boolean matches(Object get) {
//For some reason, this method is called when I assign the IsEmpty matcher. Weird, no?
if(get == null) {
return false;
}
return Arrays.equals(((Get) get).getRow(), SERIALIZATION_HELPER.getValidBytes(key));
}
}
और फिर सब कुछ ठीक काम करता है! IsNonEmpty.matches() अभी भी कॉल किया जाता है जब मैं mockHTable.get() फ़ंक्शन पर IsEmpty matcher असाइन करता हूं, लेकिन मेरे matchhers वास्तव में काम करते हैं कि उन्हें कैसे करना चाहिए।
तो सौदा क्या है? ऐसा क्यों होता है? क्या मेरा काम इस विचित्र व्यवहार की भरपाई करने के लिए एक पर्याप्त तरीका है, या क्या मैं गलत कर रहा हूं?