0
私はSpockフレームワークを使ってテストを書いています。リストの等価性をテストするときに奇妙な動きがありました。GroovyでComparableオブジェクトのリストが等価であることを確認する
私は2つの
sourceList == targetList
などのリストと、それらのリストを比較し、同じタイプの比較可能なオブジェクトが含まれ、これらのオブジェクトは、そののcompareToメソッドの代わりに、equalsを使って等しいかどうかをテストしています。
このようなリストで等価性をテストするときに、Groovyにequalsを使用させる簡単な方法はありますか?
ここでは、テストが失敗する単純なテスト仕様ですが、テストは失敗します。 シンプル方法を尋ねる
class Test extends Specification {
def "list test"() {
when:
def listA = [[index: 1, text: "1"] as Bean, [index: 2, text: "2"] as Bean]
def listB = [[index: 1, text: "1"] as Bean, [index: 2, text: "3"] as Bean]
then:
listA == listB
}
class Bean implements Comparable<Bean> {
int index
String text
@Override
public int compareTo(Bean o) {
return index.compareTo(o.index);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + index;
result = prime * result + ((text == null) ? 0 : text.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof Bean)) {
return false;
}
Bean other = (Bean) obj;
if (index != other.index) {
return false;
}
if (text == null) {
if (other.text != null) {
return false;
}
} else if (!text.equals(other.text)) {
return false;
}
return true;
}
}
}
はあなたが再現可能なサンプルを提供することはできますか? – Rao
@Rao編集された質問を参照 – Behnil
ありがとう、あなたはjavaまたはgroovyを使用していますか? – Rao