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 | Show 2 more comments1 Answer
Reset to default 8This 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.
null
. Returning 0 would mean that the mock object is equal to null, which cannot be true. – Sweeper Commented Nov 30, 2024 at 16:38myClass
is a mock object, it's not a real Comparable object. @Sweeper – walsh Commented Nov 30, 2024 at 16:42Comparable
object should behave, is there? – Sweeper Commented Nov 30, 2024 at 16:43