admin管理员组文章数量:1426295
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.
本文标签: javaWhy is the default return value of mocked Comparable object 1not 0Stack Overflow
版权声明:本文标题:java - Why is the default return value of mocked Comparable object 1, not 0? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736205929a1912117.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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