【发布时间】:2026-02-19 19:30:01
【问题描述】:
是否有比较两个不同对象的最佳做法,例如两个 Dto,如果它们不同以不显示长结果,如果对象复杂且嵌套,则很难找到不同的属性?
目前我看到一个很长的结果,很难或很麻烦地找到哪个特定属性失败了。至少它使用了一个使用 Kotlin 的数据类。
在 IDE 中,例如如果对象更大或更复杂,您必须滚动 IntelliJ 才能看到完整的比较。找出不同之处并不直观或容易。
我想避免的是例如为每个属性使用断言,并且只检查彼此的属性。
在我看来,这会导致维护开销。
例如,我可以使用 Kotest 看到这个不错的输出。为什么 Junit5 assertEquals 方法无法做到这一点?
org.opentest4j.AssertionFailedError: data class diff for com.lennykey.Cat
├ name: expected:<"Catta"> but was:<"Catty">
├ favouriteDish: expected:<"Tuna"> but was:<"Fish">
└ father: Expected null but actual was Cat(name=Father, color=orange, birthDay=1.01.1980, address=Street 1, favouriteDish=Fish, mother=null, father=null)
expected:<Cat(name=Catta, color=orange, birthDay=1.01.1980, address=Street 1, favouriteDish=Tuna, mother=null, father=null)> but was:<Cat(name=Catty, color=orange, birthDay=1.01.1980, address=Street 1, favouriteDish=Fish, mother=null, father=Cat(name=Father, color=orange, birthDay=1.01.1980, address=Street 1, favouriteDish=Fish, mother=null, father=null))>
Expected :Cat(name=Catta, color=orange, birthDay=1.01.1980, address=Street 1, favouriteDish=Tuna, mother=null, father=null)
Actual :Cat(name=Catty, color=orange, birthDay=1.01.1980, address=Street 1, favouriteDish=Fish, mother=null, father=Cat(name=Father, color=orange, birthDay=1.01.1980, address=Street 1, favouriteDish=Fish, mother=null, father=null))
【问题讨论】:
-
这是
assert...方法或测试运行程序(例如,在您的 IDE 中)所做的事情。
标签: kotlin junit junit5 kotest