java - Is there a way to match in Mockito by reference and equals at the same time? -
mockito's matchers
has eq()
uses equals
, same()
uses ==
operator.
is there way use both when comparing objects in mockito
?
let's have
list list1 = new arraylist(); list list2 = new arraylist(); system.out.println(list1.equals(list2)); //true system.out.println(list1 == list2); //false /* , want check references same, , list contents haven't changed */
so great have operator both checking ==
, .equals()
this come in handy if want check return same list, , list contents same without doing 2 assertions.
also, may classic example of 4 card problem. https://en.wikipedia.org/wiki/wason_selection_task
.equals()
can true if objects don't have same reference.
so .equals()
being true doesn't mean ==
return true , not substitute.
the equals
implementation should ==
check. if doesn´t expect bug. doing both not necessary. if let eclipse generate equals
first test ==
check example.
the same applies list, equals
of abstractlist
:
public boolean equals(object o) { if (o == this) return true; if (!(o instanceof list)) return false; listiterator<e> e1 = listiterator(); listiterator e2 = ((list) o).listiterator(); while (e1.hasnext() && e2.hasnext()) { e o1 = e1.next(); object o2 = e2.next(); if (!(o1==null ? o2==null : o1.equals(o2))) return false; } return !(e1.hasnext() || e2.hasnext()); }
Comments
Post a Comment