When I try to compare two similar lists of the same class of objects using JUnit's assertEquals
method, I get the option in Intellij:
当我尝试使用JUnit的assertEquals方法比较同一类对象的两个相似列表时,我得到了Intellij中的选项:
<Click to see difference>
The Comparison Failure dialog opens and I see the expected references and actual references. I have properly overridden the equals method because when the two objects have the same fields, the test passes.
“比较失败”对话框打开,我看到了预期的引用和实际引用。我已正确覆盖equals方法,因为当两个对象具有相同的字段时,测试通过。
Instead of showing the references, is it possible to show the fields of the objects?
而不是显示引用,是否可以显示对象的字段?
This will greatly reduce the time it takes to debug a test case.
这将大大减少调试测试用例所需的时间。
1 个解决方案
#1
2
TL;DR
Implement toString()
on Models.YTPoint
and IntelliJ will be able to display a meaningful 'difference message'.
在Models.YTPoint和IntelliJ上实现toString()将能够显示有意义的“差异消息”。
Detail
IntelliJ's 'difference message' is derived from the type's toString()
. If the type has no toString()
then IntelliJ will use the object reference.
IntelliJ的“差异消息”派生自类型的toString()。如果类型没有toString(),那么IntelliJ将使用对象引用。
So, if you define a toString()
on whatever object these lists contain then IntelliJ will be able to display a meaningful 'difference message'.
因此,如果在这些列表包含的任何对象上定义toString(),则IntelliJ将能够显示有意义的“差异消息”。
Here's an exmaple:
这是一个例子:
@Test
public void showDifferenceMessage() {
List<Foo> one = Lists.newArrayList(new Foo(1, "me"));
List<Foo> two = Lists.newArrayList(new Foo(1, "you"));
Assert.assertEquals(one, two);
}
With no implementation of Foo.toString()
when I run the test IntelliJ displays:
当我运行测试时,没有实现Foo.toString()IntelliJ显示:
java.lang.AssertionError:
Expected :[org.glytching.sandbox.Foo@16f65612]
Actual :[org.glytching.sandbox.Foo@311d617d]
And when I implement Foo.toString()
and re run that test IntelliJ displays:
当我实现Foo.toString()并重新运行IntelliJ显示的测试时:
java.lang.AssertionError:
Expected :[Foo{id=1, name='me'}]
Actual :[Foo{id=1, name='you'}]
And if I Click to see difference
IntelliJ displays:
如果我点击查看IntelliJ显示的差异:
#1
2
TL;DR
Implement toString()
on Models.YTPoint
and IntelliJ will be able to display a meaningful 'difference message'.
在Models.YTPoint和IntelliJ上实现toString()将能够显示有意义的“差异消息”。
Detail
IntelliJ's 'difference message' is derived from the type's toString()
. If the type has no toString()
then IntelliJ will use the object reference.
IntelliJ的“差异消息”派生自类型的toString()。如果类型没有toString(),那么IntelliJ将使用对象引用。
So, if you define a toString()
on whatever object these lists contain then IntelliJ will be able to display a meaningful 'difference message'.
因此,如果在这些列表包含的任何对象上定义toString(),则IntelliJ将能够显示有意义的“差异消息”。
Here's an exmaple:
这是一个例子:
@Test
public void showDifferenceMessage() {
List<Foo> one = Lists.newArrayList(new Foo(1, "me"));
List<Foo> two = Lists.newArrayList(new Foo(1, "you"));
Assert.assertEquals(one, two);
}
With no implementation of Foo.toString()
when I run the test IntelliJ displays:
当我运行测试时,没有实现Foo.toString()IntelliJ显示:
java.lang.AssertionError:
Expected :[org.glytching.sandbox.Foo@16f65612]
Actual :[org.glytching.sandbox.Foo@311d617d]
And when I implement Foo.toString()
and re run that test IntelliJ displays:
当我实现Foo.toString()并重新运行IntelliJ显示的测试时:
java.lang.AssertionError:
Expected :[Foo{id=1, name='me'}]
Actual :[Foo{id=1, name='you'}]
And if I Click to see difference
IntelliJ displays:
如果我点击查看IntelliJ显示的差异: