简体   繁体   English

单元测试 - AssertionFailedError - Java

[英]Unit Testing - AssertionFailedError - Java

I'm having troubles understanding why this unit test is not working.我无法理解为什么这个单元测试不起作用。 I'm creating two instances of @RegionWithActivities and I thought that it should pass the test.我正在创建 @RegionWithActivities 的两个实例,我认为它应该通过测试。

    @Test
    void testAreEqual()
    {
        RegionWithActivities regionWithActivities1 = new RegionWithActivities(4, regions);
        RegionWithActivities regionWithActivities2 = new RegionWithActivities(4, regions);

        assertEquals(regionWithActivities1, regionWithActivities2);
    }

The error is:错误是:

org.opentest4j.AssertionFailedError: expected: com.luciaandres.analysis.RegionWithActivities@710726a3<RegionWithActivities{numberOfActivities=4, regionIds=[4DA19B2B1328127FC062FB79F6F435A5, B66FA66DA650717E0964A4E30A716DAE, C841C0BED1CCDD643955065A696EED34, F2D04F76EA1EBD6C8E3AEDD506FBA35A]}> but was: com.luciaandres.analysis.RegionWithActivities@646007f4<RegionWithActivities{numberOfActivities=4, regionIds=[4DA19B2B1328127FC062FB79F6F435A5, B66FA66DA650717E0964A4E30A716DAE, C841C0BED1CCDD643955065A696EED34, F2D04F76EA1EBD6C8E3AEDD506FBA35A]}>
Expected :RegionWithActivities{numberOfActivities=4, regionIds=[4DA19B2B1328127FC062FB79F6F435A5, B66FA66DA650717E0964A4E30A716DAE, C841C0BED1CCDD643955065A696EED34, F2D04F76EA1EBD6C8E3AEDD506FBA35A]}
Actual   :RegionWithActivities{numberOfActivities=4, regionIds=[4DA19B2B1328127FC062FB79F6F435A5, B66FA66DA650717E0964A4E30A716DAE, C841C0BED1CCDD643955065A696EED34, F2D04F76EA1EBD6C8E3AEDD506FBA35A]}


    at RegionWithActivitiesTest.testAreEqual(RegionWithActivitiesTest.java:29)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1507)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1507)

Could you please explain me?你能解释一下吗? Thanks!谢谢!

When you use assertEquals ,it tries to compare two objects using equals() method.If you have not overriden equals() method from Object class in your class,the it uses the default equals() method from Object class which has implementation as below :当您使用 assertEquals 时,它会尝试使用 equals() 方法比较两个对象。如果您没有在类中覆盖 Object 类中的 equals() 方法,它将使用 Object 类中的默认 equals() 方法,其实现如下:

public boolean equals(Object obj) {
       return (this == obj);    
}

As you can see, here it just compares object reference and because two objects have two different references in your case, the assertion is failing, so you need to implement equals() method in RegionWithActivities class and as a best practice when you override equals(), you should override hashcode() method as well, but it is not necessary for your assertion to pass.如您所见,这里只是比较对象引用,并且因为在您的情况下两个对象有两个不同的引用,所以断言失败,因此您需要在 RegionWithActivities 类中实现 equals() 方法,并作为重写 equals() 时的最佳实践),您也应该覆盖 hashcode() 方法,但您的断言没有必要通过。

Theoretically, it's possible to just implement equals in RegionWithActivities to make your test case pass as assertEquals asserts based on equals() of RegionWithActivities .理论上,可以只在RegionWithActivities实现equals以使您的测试用例作为基于RegionWithActivities equals()assertEquals断言通过。 But implementing just equals() and not implementing hashcode() would violet Object class contract 2 as mentioned below -但是仅实现equals()而没有实现hashcode()将紫罗兰色对象类合同2 如下所述-

The general contract of hashCode is: hashCode 的总合约为:

  1. Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified.在 Java 应用程序执行期间,只要在同一个对象上多次调用它,hashCode 方法必须始终返回相同的整数,前提是在对象的 equals 比较中使用的信息没有被修改。 This integer need not remain consistent from one execution of an application to another execution of the same application.该整数不需要从应用程序的一次执行到同一应用程序的另一次执行保持一致。
  2. If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.如果根据 equals(Object) 方法两个对象相等,则对两个对象中的每一个调用 hashCode 方法必须产生相同的整数结果。
  3. It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results.如果根据 equals(java.lang.Object) 方法两个对象不相等,则不需要对两个对象中的每一个调用 hashCode 方法必须产生不同的整数结果。 However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.但是,程序员应该意识到为不相等的对象生成不同的整数结果可能会提高哈希表的性能。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM