简体   繁体   English

比较两个对象时,JUnit assertEquals()不起作用

[英]JUnit assertEquals() not working when comparing two objects

I'm trying to get the hang of Java. 我正试图掌握Java。 Unit testing is pretty important to me and so recently I've started to use JUnit. 单元测试对我来说非常重要,所以最近我开始使用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. 基本上,当我在一个类中创建一个类的新实例的方法时,我尝试测试该方法,我得到一个奇怪的错误。

"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's the link to my classes on github. 这是我在github上的类的链接。 The method I am trying to test is the mt() method, and the test class is ILogTest . 我试图测试的方法是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错误(即使对象更复杂 - 带参数)

assertEquals will use Object#equals for each object being compared. assertEquals将为每个被比较的对象使用Object#equals 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. 看起来你的类ILogTest不会覆盖equals方法,所以调用Object#equals只会比较引用本身,因为它们是不同的对象引用,结果将是false。

You have two options: 您有两种选择:

  1. Override public boolean equals(Object o) in ILogTest . ILogTest覆盖public boolean equals(Object o)
  2. Use assertEquals on the relevant fields that implement equals method eg String , Integer , Long , etc. This one requires more code but is useful when you cannot modify the class(es) being asserted. 在实现equals方法的相关字段上使用assertEquals ,例如StringIntegerLong等。这个需要更多代码,但是当你不能修改被断言的类时它很有用。

If you are using a modern IDE for development (like Eclipse, IntelliJ etc), they can generate these methods for you. 如果您使用现代IDE进行开发(如Eclipse,IntelliJ等),他们可以为您生成这些方法。 Check that out for two reasons: 1) to save time 2) To prevent possible bugs. 检查出来有两个原因: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 to implement the other one as well. 还有一件事,当你实现这两个时,你也必须实现另一个。

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如果不为false则为true,所以你需要写下一个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方法?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Junit 4 assertEquals(...)无法比较两个对象 - Junit 4 assertEquals(…) fails to compare two objects 比较大字符串时,JUnit assertEquals的更详细的输出 - More verbose output for JUnit assertEquals when comparing big strings 比较布尔值时,JUnit Mockito在assertEquals中始终返回false - JUnit Mockito always returns false in assertEquals when comparing boolean values JUnit4(assertEquals)说对象不同,当它们相同时? - JUnit4 (assertEquals) saying objects are different, when they're the same? 在两个对象上的assertEquals() - assertEquals() on two objects 在Junit测试中,如何比较两个ArrayList <Double> 使用assertEquals(),还有更好的选择吗? - In Junit testing, how to comparing two ArrayList<Double> using assertEquals(), and is there an better alternative? Java / JUnit - 比较两个多项式对象 - Java / JUnit - comparing two polynomial objects 用JUnit比较两个对象显示出奇怪的行为 - Comparing two objects with JUnit shows strange behaviour 如何使用assertEquals或JUNIT测试中的任何其他方式比较两个对象? - How to compare two objects using assertEquals or by any other way in JUNIT testing? JUnit有什么更好的方法在两个对象之间使用assertEquals而不定义equals方法 - JUnit what better way to use assertEquals between two objects without defining equals method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM