Should Mockito eq call equal?

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 is equal to inherited from AbstractList, calls it memebers "equals", and "Bucket" implements "equals". However, the debugger never stops in the Bucket equals method. Did I miss something?

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

+5
source share
1 answer

In fact, org.mockito.Matchers.eq uses the org.mockito.internal.matchers.Equality.areEqual(Object o1, Object o2) method to check if this matching element is equal to the actual value passed to the method. Interestingly, the implementation of this method was stolen from hamcrest, as indicated in the 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 happens in your own code. Since you are passing an ArrayList the eq() argument , you may need to dig deeper into the areArraysEqual and areArrayLengthsEqual .

+7
source

All Articles