Java 即使所有字段都相同,地址相等也会失败
我创建了一个如下所示的Java 即使所有字段都相同,地址相等也会失败,java,unit-testing,equality,Java,Unit Testing,Equality,我创建了一个如下所示的Address类,然后我想检查两个Addresses的相等性。如果所有字段都相同,则认为这两个Addresses是相同的 因此,我实现了hashCode和equals方法 public class Address{ public String addressLine1; public String addressLine2; public String city; public String state; publ
Address
类,然后我想检查两个Address
es的相等性。如果所有字段都相同,则认为这两个Address
es是相同的
因此,我实现了hashCode
和equals
方法
public class Address{
public String addressLine1;
public String addressLine2;
public String city;
public String state;
public String pincode;
public String phoneNumber;
public String country;
public Address() {
}
public Address(String addressLine1, String addressLine2, String city,
String state, String pincode, String phoneNumber, String country) {
this.addressLine1 = addressLine1;
this.addressLine2 = addressLine2;
this.city = city;
this.state = state;
this.pincode = pincode;
this.phoneNumber = phoneNumber;
this.country = country;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result
+ ((addressLine1 == null) ? 0 : addressLine1.hashCode());
result = prime * result
+ ((addressLine2 == null) ? 0 : addressLine2.hashCode());
result = prime * result + ((city == null) ? 0 : city.hashCode());
result = prime * result + ((country == null) ? 0 : country.hashCode());
result = prime * result
+ ((phoneNumber == null) ? 0 : phoneNumber.hashCode());
result = prime * result + ((pincode == null) ? 0 : pincode.hashCode());
result = prime * result + ((state == null) ? 0 : state.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
System.out.println("equals("+obj);
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
Address other = (Address) obj;
if (addressLine1 == null) {
if (other.addressLine1 != null)
return false;
} else if (!addressLine1.equals(other.addressLine1))
return false;
if (addressLine2 == null) {
if (other.addressLine2 != null)
return false;
} else if (!addressLine2.equals(other.addressLine2))
return false;
if (city == null) {
if (other.city != null)
return false;
} else if (!city.equals(other.city))
return false;
if (country == null) {
if (other.country != null)
return false;
} else if (!country.equals(other.country))
return false;
if (phoneNumber == null) {
if (other.phoneNumber != null)
return false;
} else if (!phoneNumber.equals(other.phoneNumber))
return false;
if (pincode == null) {
if (other.pincode != null)
return false;
} else if (!pincode.equals(other.pincode))
return false;
if (state == null) {
if (other.state != null)
return false;
} else if (!state.equals(other.state))
return false;
return true;
}
public String toString() {
return this.addressLine1+","+this.addressLine2+","+this.city+","+this.state+","+this.pincode+","+this.country+","+this.phoneNumber;
}
}
在这个类的测试用例中,我尝试创建两个相同的地址并调用assertEquals。但是,这失败了
类AddressTests扩展了UnitTest{
@Test
public void testAddressEquality() {
Address address1 = new Address();
address1.addressLine1 = "#1000,South Avenue";
address1.state = "New York";
address1.country = "U.S";
System.out.println("address1="+address1);
Address address2 = new Address();
address2.addressLine1 = "#1000,South Avenue";
address2.state = "New York";
address2.country = "U.S";
System.out.println("address2="+address2);
assertEquals(address1,address2);
}
}
assertEquals失败
Failure, expected: models.Address<#1000,South Avenue,null,null,New York,null,U.S,null> but was: models.Address<#1000,South Avenue,null,null,New York,null,U.S,null>
失败,应为:models.Address,但为:models.Address
有人能帮我理解为什么失败吗?这是一个问题:
if (!super.equals(obj))
return false;
Object.equals
检查引用是否相同(与您的this==obj
测试相同)。你根本不想在那里检查——你已经测试了引用的平等性,如果对象引用不一样,你也不想退出
表格:
类对象的equals方法实现了对象上最有区别的等价关系;也就是说,对于任何非空引用值x和y,当且仅当x和y引用同一对象(x==y的值为true)时,此方法才返回true
一个实现():
这是一个问题:
if (!super.equals(obj))
return false;
Object.equals
检查引用是否相同(与您的this==obj
测试相同)。你根本不想在那里检查——你已经测试了引用的平等性,如果对象引用不一样,你也不想退出
表格:
类对象的equals方法实现了对象上最有区别的等价关系;也就是说,对于任何非空引用值x和y,当且仅当x和y引用同一对象(x==y的值为true)时,此方法才返回true
一个实现():
我认为你也应该修改你的散列函数。为什么不直接取其他散列值的和呢?按照您的方式,如果大多数字段都不是空的,那么结果将超过20亿(Integer.MAX_值),因此您需要环绕整数值 我认为您还应该修改哈希函数。为什么不直接取其他散列值的和呢?按照您的方式,如果大多数字段都不是空的,那么结果将超过20亿(Integer.MAX_值),因此您需要环绕整数值 您应该永远不要手动创建hashCode/equals。。。让您的IDE为您选择
在IntelliJ IDEA中,简单地使用ALT+Insert,生成hashCode/equals。Eclipse在某种程度上是类似的。您应该永远不要手动创建hashCode/equals。。。让您的IDE为您选择
在IntelliJ IDEA中,简单地使用ALT+Insert,生成hashCode/equals。Eclipse在某种程度上是类似的。您是否在调试器中通过了测试以找出哪一行失败了?感谢您的建议..我发现'if(!super.equals(obj))返回false;'返回false的行是否错误?这是错误的吗?您是否在调试器中通过了测试以找出哪一行失败?感谢您的建议..我发现“if(!super.equals(obj))返回false;”返回false的行是不是错了?