Search code examples
javamockitocomparabledefault-return-value

Why is the default return value of mocked Comparable object 1, not 0?


I have a class that implements Comparable interface

public class MyClass implements java.lang.Comparable<MyClass>{
  @Override
  public int compareTo(MyClass o) {
    return 2;
  }
}

I wrote a test class to check the mock object default return value, and the version of Mockito framework is 5.14.2

@Test
void testMyClass() {
  var myClass = mock(MyClass.class);
  assertEquals(0, myClass.compareTo(new MyClass()));
}

Unfortunately, this test failed. JUnit told me the compareTo method returns 1, not 0.

However, when I replaced the java.lang.Comparable with my custom Comparable interface, which is totally the same as the java.lang.Comparable interface. This test passed!.

Could anybody please explain this to me?


Solution

  • This is documented behaviour in ReturnsEmptyValues, which is the default answer for every mock.

    Returns zero if references are equals otherwise non-zero for Comparable#compareTo(T other) method (see issue 184)

    So it will only return 0 if you wrote myClass.compareTo(myClass).

    See also the source code, where there is a special case for isCompareToMethod being true.

    It doesn't make sense for compareTo(null) to return 0 because no object can be equal to null. Although a mock object cannot fulfil all the requirements of a compareTo implementation, it is still useful to have at least some sensible behaviour. This is essentially making compareTo "consistent with equals".

    "Issue 184" refers to this, where it was expected that a TreeSet should be able to contain multiple mock objects at the same time. If compareTo always returned 0, this would not be possible.