最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Why is the default return value of mocked Comparable object 1, not 0? - Stack Overflow

programmeradmin4浏览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, myClasspareTo(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?

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?

Share Improve this question edited Nov 30, 2024 at 16:58 walsh asked Nov 30, 2024 at 16:20 walshwalsh 3,2732 gold badges18 silver badges32 bronze badges 7
  • 1 Would you not want it to return 1? It is a sensible value to return because a non-null thing can never be equal to null. Returning 0 would mean that the mock object is equal to null, which cannot be true. – Sweeper Commented Nov 30, 2024 at 16:38
  • Where are you mocking the return value? – Zahid Khan Commented Nov 30, 2024 at 16:39
  • myClass is a mock object, it's not a real Comparable object. @Sweeper – walsh Commented Nov 30, 2024 at 16:42
  • I know, but there are no downsides to it behaving like how a Comparable object should behave, is there? – Sweeper Commented Nov 30, 2024 at 16:43
  • myClass is a mock object, when it was created, Mockito assigned a default return value for every methods. @ZahidKhan – walsh Commented Nov 30, 2024 at 16:46
 |  Show 2 more comments

1 Answer 1

Reset to default 8

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.

发布评论

评论列表(0)

  1. 暂无评论