java 比较两个对象时 JUnit assertEquals() 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31032686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JUnit assertEquals() not working when comparing two objects
提问by David Anekstein
I'm trying to get the hang of Java. Unit testing is pretty important to me and so recently I've started to use JUnit. It was tough to begin with but I'm getting the hang of it. All of my tests have worked up to this point, with the exception of comparing two objects of the same class (I haven't tried testing a function that creates an object of a different class). Basically, when I have a method within a class that creates a new instance of the class, and I try to test the method, I get a weird error.
我正在尝试掌握 Java 的窍门。单元测试对我来说非常重要,所以最近我开始使用 JUnit。一开始很难,但我已经掌握了窍门。到目前为止,我的所有测试都已完成,除了比较同一类的两个对象(我还没有尝试测试创建不同类对象的函数)。基本上,当我在一个类中有一个方法来创建该类的新实例时,我尝试测试该方法时,我会收到一个奇怪的错误。
"expected:runnersLog.MTLog@433c675d but was runnersLog.MTLog@3f91beef"
“预期:runnersLog.MTLog@433c675d 但 runnersLog.MTLog@3f91beef”
I have tried researching this problem, but haven't found anything of much help. Here'sthe link to my classes on github. The method I am trying to test is the mt()
method, and the test class is ILogTest
.
我试过研究这个问题,但没有找到任何帮助。这是我在 github 上的课程的链接。我要测试的mt()
方法是方法,测试类是ILogTest
.
This is not the only case where I am having this problem. With any class that has a method that returns a new object of the same class, I am getting this exact same 3f91beef error (even when the object is more complicated - with arguments)
这不是我遇到此问题的唯一情况。对于任何具有返回同一类的新对象的方法的类,我都会收到完全相同的 3f91beef 错误(即使对象更复杂 - 带有参数)
回答by Luiggi Mendoza
assertEquals
will use Object#equals
for each object being compared. Looks like your class ILogTest
doesn't override equals
method, so calling Object#equals
will just compare the references by itself, and since they're different object references, the result will be false.
assertEquals
将Object#equals
用于要比较的每个对象。看起来你的类ILogTest
没有覆盖equals
方法,所以调用Object#equals
只会自己比较引用,并且由于它们是不同的对象引用,结果将是错误的。
You have two options:
您有两个选择:
- Override
public boolean equals(Object o)
inILogTest
. - Use
assertEquals
on the relevant fields that implementequals
method e.g.String
,Integer
,Long
, etc. This one requires more code but is useful when you cannot modify the class(es) being asserted.
- 覆盖
public boolean equals(Object o)
在ILogTest
。 assertEquals
在实现equals
方法的相关字段上使用,例如String
,Integer
,Long
等。这个需要更多的代码但是当你不能修改被断言的类时很有用。
回答by Alp
If you are using a modern IDE for development (like Eclipse, IntelliJ etc), they can generatethese methods for you. Check that out for two reasons: 1) to save time 2) To prevent possible bugs.
如果您使用现代 IDE 进行开发(如 Eclipse、IntelliJ 等),他们可以为您生成这些方法。检查出来有两个原因:1) 节省时间 2) 防止可能的错误。
In eclipse IDE, you can do so by selecting source -> generate hashCode() and equals().
在 Eclipse IDE 中,您可以通过选择 source -> generate hashCode() 和 equals() 来实现。
One more thing, when you implement of of these two, you have toimplement the other one as well.
还有一件事,当您实现这两个中的一个时,您还必须实现另一个。
回答by DguezTorresEmmanuel
You need to overrride equals, the equals method in the superclass Object checks for references if both references point to the same object equals is true if not false, so you need to write down an equals method that will check your objects content and check if the values are the same, it is also recommended that you override your hashCode method too.
您需要重写equals,超类Object 中的equals 方法检查引用是否两个引用都指向同一个对象equals 为true 如果不是false,因此您需要写下一个equals 方法来检查您的对象内容并检查是否值相同,还建议您也覆盖 hashCode 方法。
An example could be:
一个例子可能是:
Custom a= new Custom("");
Custom b= a;
//b would be equal a. because they reference the same object.
Custom c= new Custom("");
//c would not be equal to a, although the value is the same.
to learn more you could check: Why do I need to override the equals and hashCode methods in Java?
要了解更多信息,您可以查看: 为什么我需要覆盖 Java 中的 equals 和 hashCode 方法?