Java 测试对象的两个实例是否相等 JUnit

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27605714/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:41:27  来源:igfitidea点击:

Test two instances of object are equal JUnit

javajunit

提问by thirstyladagain

I have two objects Aand B. Object Ais actual result I get during JUnit test. Object Bis I do a actual end db call and assume as expected review. I need to assert that two object instances Aand Bare equal in valueand not actual object.

我有两个对象AB. 对象A是我在 JUnit 测试期间得到的实际结果。对象B是我做一个实际的结束数据库调用并假设为预期的。我需要断言两个对象实例AB相等的价值,而不是实际的对象。

We have a method called assertEquals(obj expected, obj actual)in JUnit but I don't want that way.

我们有一个assertEquals(obj expected, obj actual)在 JUnit 中调用的方法,但我不想要那样。

Is there a way or workaround to achieve the same?

有没有办法或解决方法来实现相同的目标?

回答by aurelius

using hamcrest, you can assert each members of those 2 objects!

使用 hamcrest,您可以断言这两个对象的每个成员!

e.g. assertThat(actual).isEqualToComparingFieldByField(expected);

例如 assertThat(actual).isEqualToComparingFieldByField(expected);

回答by Predrag Maric

assertEquals()calls equals()on your objects, and there is no way around that. What you can do is to implement something like public boolean like(MyClass b)in your class, in which you would compare whatever you want. Then, you could check the result using assertTrue(a.like(b)).

assertEquals()调用equals()你的对象,这是没有办法的。你可以做的是public boolean like(MyClass b)在你的课堂上实现类似的东西,你可以在其中比较任何你想要的东西。然后,您可以使用assertTrue(a.like(b)).

回答by sol4me

You will need to override the equalsand for good practise hashCodemethod for your Class. After that assertEquals(obj expected, obj actual)will not check for reference but will use the logic that you have implemented inequalsmethod.

您将需要为您的班级覆盖equalsand for good practicehashCode方法。之后assertEquals(obj expected, obj actual)将不会检查参考,而是会使用您在equals方法中实现的逻辑。

回答by tddmonkey

Think about exactly what it is you're trying to achieve. Do you want to test object identity or verify that the two objects have exactly the same data? From your suggestion of assertEquals I am guessing you want to go field-by-field.

仔细想想你想要达到的目标。您是要测试对象身份还是验证两个对象是否具有完全相同的数据?根据您对 assertEquals 的建议,我猜您想逐场进行。

If the objects are shallow you can use

如果对象很浅,您可以使用

assertThat(actual, samePropertyValuesAs(expected));

This fails when the object is a composite though. If you want to walk the entire graph you can use the sameBeanAsmatcher we wrote some time back to address this issue:

但是,当对象是复合对象时,这会失败。如果你想遍历整个图,你可以使用sameBeanAs匹配器,我们写了一些时间来解决这个问题:

assertThat(actual, sameBeanAs(expected));

If you're using AssertJ then you can also use the built-in functionality like this:

如果您使用的是 AssertJ,那么您还可以使用内置功能,如下所示:

assertThat(actual).isEqualToComparingFieldByField(expected);

One thing you don't want to do it override the equalsmethod for this as that might change in the future to accommodate business need.

您不想做的一件事是覆盖此equals方法,因为将来可能会更改以适应业务需求。

回答by acdcjunior

If you want deep equality, you can use Commons Lang3 EqualsBuilder#reflectionEquals():

如果你想要深度平等,你可以使用 Commons Lang3 EqualsBuilder#reflectionEquals()

Assert.assertTrue(EqualsBuilder.reflectionEquals(expected,actual));