为什么 .equals 方法在两个相同的值对象上失败?
我创建了一个值对象 MarketVO
并且该值对象的两个实例具有相同的元素,并且每个元素具有相同的值。
我的值对象类是:
public class MarketVO {
private double floatAmt;
private Date marketDate;
private long marketCap;
}
以下是值:
returnedData:
FloatAmt: 247657.5418618201, MarketCap: 5249164,
MarketDate: 2011-07-29 00:00:00.0
ExpectedData:
FloatAmt: 247657.5418618201, MarketCap: 5249164,
MarketDate: 2011-07-29 00:00:00.0
现在在我的单元测试类中,我想断言我的返回类型和预期类型相同,包含相同顺序的相同值,因此我正在执行类似
assertTrue( returnedData.equals(expectedData))
,现在返回 false
值,但如果我
assertEquals(testObject.getfloatAmt(), testObject2.getfloatAmt());
assertEquals(testObject.getmarketCap(), testObject2.getmarketCap());
assertEquals(testObject.getmarketDate(), testObject2.getmarketDate());
这样做测试通过,所以不确定为什么 .equals
方法不在这儿工作吗?有什么建议吗?
更新:我想在这里强调一下,我们正在使用它来进行单元测试。
I have created a value object MarketVO
and two instances of this value object have same elements and same value for each element.
My value object class is:
public class MarketVO {
private double floatAmt;
private Date marketDate;
private long marketCap;
}
Here are the values:
returnedData:
FloatAmt: 247657.5418618201, MarketCap: 5249164,
MarketDate: 2011-07-29 00:00:00.0
expectedData:
FloatAmt: 247657.5418618201, MarketCap: 5249164,
MarketDate: 2011-07-29 00:00:00.0
Now in my unit test class, I want to assert that my returned and expected type is same containing same value in same order so am doing something like
assertTrue(returnedData.equals(expectedData))
, now this is returning false
value but if I do
assertEquals(testObject.getfloatAmt(), testObject2.getfloatAmt());
assertEquals(testObject.getmarketCap(), testObject2.getmarketCap());
assertEquals(testObject.getmarketDate(), testObject2.getmarketDate());
this test passes and so am not sure as to why .equals
method is not working in here? Any suggestions?
Update: I want to put emphasize here that we are using this for doing Unit Testing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
.equals
比较对象引用,而不是对象内容。您可能想重写 equals(和 hashCode)方法。像这样的东西:
The default implementation of
.equals
compares object references, not object content.You probably want to override the equals (and hashCode) methods. Something like this:
而是使用 org.apache.commons 库,它为您提供了复杂的方法来很好地实现这些有价值的方法。同一个库还包含 ToStringBuilder,这也非常方便。
Maven依赖=> commons-lang3 (org.apache.commons)
Use rather the org.apache.commons library which gives you sophisticated ways implement those valuable methods well. The same library also contains ToStringBuilder which is very handy too.
Maven dependency => commons-lang3 (org.apache.commons)
equals
方法不起作用,因为您没有根据所需的行为重写它。Object
(您的类继承自)的默认行为是比较引用。两个不同的实例具有不同的引用,因此equals
失败。The
equals
method doesn't work as you didn't override it for your required behaviour. The default behaviour onObject
(which your class inherits from) is to compare references. Two different instances have different references, thus theequals
fails.默认情况下,
.equals()
检查身份,而不是相等。更改此代码并将其添加到您的类中By default
.equals()
checks identity and not equality. Change and add this code to your class我强烈建议使用 Lombok 注释
@EqualsAndHashcode
,它确实有助于避免equals
和hashCode
方法的错误。默认情况下,它使用所有非静态非瞬态字段创建 equals 和 hashCode 方法,但您可以指定其他行为,例如排除某些字段:
或仅包含某些字段:
I highly recommend using Lombok annotation
@EqualsAndHashcode
, it really helps to avoid bugs withequals
andhashCode
methods.It creates equals and hashCode methods using all non-static non-transient fields by default, but you can specify other behaviors like excluding some fields:
or including only some fields:
哈希值不同,因为它们是不同的实例
the hashes are different because they are different instances
更准确地说,默认行为是比较内存中对象的地址(如果引用指向内存中完全相同的对象(地址),则为 true)。因此重写这些方法以获得所需的行为。
More precisely the default behaviour is to compare object's addresses in memory (true if the references point to the exactly same object (address) in memory). So override these methods the get the required behaviour.