Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shouldn't Mockito's eq call equals?

List<Bucket> bucketList = new ArrayList<Bucket>(50);
// Populate bucketList and use it to run the test
verify(mockDao).createSomething(anyMapOf(Long.class, Long.class), eq(bucketList));

ArrayList's equals inherited from AbstractList calls its memebers' "equals" and "Bucket" does implement "equals". However, the debugger never stops in the Bucket's equals method. Am I missing something?

By the way, "eq" is org.mockito.Matchers.eq.

like image 726
Amir Keibi Avatar asked Jul 06 '15 18:07

Amir Keibi


1 Answers

Actually org.mockito.Matchers.eq uses org.mockito.internal.matchers.Equality.areEqual(Object o1, Object o2) method to check equality of given matcher to actual value passed to the method. What is interesting, the implementation of this method was stolen from hamcrest as is stated in comment:

//stolen from hamcrest because I didn't want to have more dependency than Matcher class 
//...
public static boolean areEqual(Object o1, Object o2) {
    if (o1 == o2 ) {
        return true;
} else if (o1 == null || o2 == null) {
        return o1 == null && o2 == null;
    } else if (isArray(o1)) {
        return isArray(o2) && areArraysEqual(o1, o2);
    } else {
        return o1.equals(o2);
    }
}

Place a breakpoint at the very beginning of this method to see what is happening in your own code. Since you pass ArrayList to the argument of eq() you may need to dig deeper into areArraysEqual and areArrayLengthsEqual methods.

like image 194
kukis Avatar answered Nov 15 '22 09:11

kukis