equality of Addresses fails even if all fields are identical

I created an `Address` class like below,Then I wanted to check equality of two `Address`es.If all the fields are identical,the two `Address`es are considered identical. So,I implemented the `hashCode` and `equals` methods. 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; } } In the TestCase for this class ,I tried to create two identical addresses and calls assertEquals..This fails however.. class AddressTests extends 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 fails 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> Can someone help me understand why this failed?
Have you stepped through the test in a debugger to find out which line fails?

以上就是equality of Addresses fails even if all fields are identical的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏