- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 3.14.0
版本的 org.assertj.core.api.Assertions.assertThat
比较两个对象:
assertThat(getActual())
.usingRecursiveComparison()
.ignoringFields("customer", "orders", "\$hashCode", "fragments.parentFields.__typename", "fragments.parentFields.image")
.isEqualTo(orderForm)
当我这样做时,出现以下错误:
when recursively comparing field by field, but found the following difference:
field/property 'fragments' differ:
- actual value : Fragments{parentFields=Optional[ParentFields{__typename=OrderForm, id=1, image=Image{__typename=Image, id=71, accuracy=100.0, type=ORDER_FORM, state=AUTOMATIC, value=order form, base64=}}]}
- expected value : Fragments{parentFields=Optional[ParentFields{__typename=ParentFields, id=1, image=Image{__typename=Image, id=123, accuracy=99.99, type=NONE, state=NONE, value=value, base64=base64}}]}
The recursive comparison was performed with this configuration:
- the following fields were ignored in the comparison: customer, orders, $hashCode, fragments.parentFields.__typename, fragments.parentFields.image
- overridden equals methods were used in the comparison
- these types were compared with the following comparators:
- java.lang.Double -> DoubleComparator[precision=1.0E-15]
- java.lang.Float -> FloatComparator[precision=1.0E-6]
- actual and expected objects and their fields were compared field by field recursively even if they were not of the same type, this allows for example to compare a Person to a PersonDto (call strictTypeChecking(true) to change that behavior).
根据我的理解,忽略我添加到 ignoreingFields
函数中的所有字段,比较应该是:
- actual value : Fragments{parentFields=Optional[ParentFields{id=1}]}
- expected value : Fragments{parentFields=Optional[ParentFields{id=1}]}
因此它不应该失败,它应该成功。
其他被忽略的字段(customer
、orders
、\$hashCode
)被正确忽略。所以我想知道嵌套对象的字段是否有问题。但是在 javadoc 中我发现:
* // assertion succeeds as name and home.address.street fields are ignored in the comparison
* assertThat(sherlock).usingRecursiveComparison()
* .ignoringFields("name", "home.address.street")
* .isEqualTo(noName);
所以我认为我正在按照 API 的预期使用它。
当然,我可以通过简单地执行以下操作来解决所有这些问题:
assertThat(actual
?.fragments()
?.parentFields()
?.get()
?.id()
).isEqualTo(orderForm
.fragments()
.parentFields()
.get()
.id())
但是我有很多对象,我必须像那样比较并且它们没有相同的父类(super class)。所以我要创建类似的东西:
fun assertCommons(actual: Any, expected: Any) {
assertThat(actual)
.usingRecursiveComparison()
....
}
回到问题:忽略这些字段有什么问题?
编辑
由于 parentFields
是可选的:
...
.ignoringFields("customer", "orders", "\$hashCode", "fragments.parentFields.get().__typename", "fragments.parentFields.get().image")
...
field/property 'fragments' differ:
- actual value : Fragments{parentFields=Optional[ParentFields{__typename=OrderForm, id=1, image=Image{__typename=Image, id=71, accuracy=100.0, type=ORDER_FORM, state=AUTOMATIC, value=order form, base64=}}]}
- expected value : Fragments{parentFields=Optional[ParentFields{__typename=ParentFields, id=1, image=Image{__typename=Image, id=123, accuracy=99.99, type=NONE, state=NONE, value=value, base64=base64}}]}
The recursive comparison was performed with this configuration:
- the following fields were ignored in the comparison: customer, orders, $hashCode, fragments.parentFields.get().__typename, fragments.parentFields.get().image
- overridden equals methods were used in the comparison
- these types were compared with the following comparators:
- java.lang.Double -> DoubleComparator[precision=1.0E-15]
- java.lang.Float -> FloatComparator[precision=1.0E-6]
- actual and expected objects and their fields were compared field by field recursively even if they were not of the same type, this allows for example to compare a Person to a PersonDto (call strictTypeChecking(true) to change that behavior).
使用 value
而不是 get()
...
.ignoringFields("customer", "orders", "\$hashCode", "fragments.parentFields.value.__typename", "fragments.parentFields.value.image")
...
when recursively comparing field by field, but found the following difference:
field/property 'fragments' differ:
- actual value : Fragments{parentFields=Optional[ParentFields{__typename=OrderForm, id=1, image=Image{__typename=Image, id=71, accuracy=100.0, type=ORDER_FORM, state=AUTOMATIC, value=order form, base64=Optional[]}}]}
- expected value : Fragments{parentFields=Optional[ParentFields{__typename=ParentFields, id=1, image=Image{__typename=Image, id=123, accuracy=99.99, type=NONE, state=NONE, value=value, base64=Optional[base64]}}]}
The recursive comparison was performed with this configuration:
- the following fields were ignored in the comparison: customer, orders, $hashCode, fragments.parentFields.value
- overridden equals methods were used in the comparison
- these types were compared with the following comparators:
- java.lang.Double -> DoubleComparator[precision=1.0E-15]
- java.lang.Float -> FloatComparator[precision=1.0E-6]
- actual and expected objects and their fields were compared field by field recursively even if they were not of the same type, this allows for example to compare a Person to a PersonDto (call strictTypeChecking(true) to change that behavior).
最佳答案
"fragments.parentFields.get().__typename"
不是您打算执行的有效声明。当指定 "a.b.c"
时,AssertJ 会尝试查找 getA().getB().getC()
或 a.b.c
或否字段/getter 的组合(如 getA().b.getC()
。
指定 fragments.parentFields.get()
将不起作用,因为没有名为 get()
的字段。尝试使用 value
代替,因为这是 get()
返回的字段(AssertJ 可以读取私有(private)字段)。
关于kotlin - AssertJ: `ignoringFields` 是不是忽略了所有提供的字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59156118/
此处,“OK”当然意味着 AYOR(风险由您自己承担),但如果避免与现有属性名称发生明显冲突,则不会出现可预见的问题。 Skyfield物体——尤其是行星——通常具有有限数量的属性。我经常编写简短的脚
我们有一个(假设)将在 1-2 年内完成的项目。到那时,JDK7(希望还有 Java7 JCP 规范)应该准备就绪。 但是,我想知道,甲骨文做出“愚蠢”决定的“危险”有多大,这会使 JDK7 成为一个
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 6 年前。 Improv
要知道,haswell是英特尔作为Ivy Bridge微架构的“第四代核心”继承者而开发的一种处理器微架构的代号。 1英特尔正式发布了基于这种微架构的CPU... More 但是,我想知道如何通过在
修改 setter 参数是否正常?假设我们有 setString 方法。我们真的想保留字符串的修剪形式。所以带有尾随空格的字符串是无效的,但我们不想抛出异常。 最好的解决办法是什么?修剪 setter
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 去年关闭。 Improve this
我写了一段代码,然后编译它没有错误。但是当我运行时,它会引发异常(java.lang.NoClassDefFoundError: TesKt$test$1$1)。 这是语言错误吗? private f
这道题是从算法导论的习题23.1-7演化而来的。 原问题是: 23.1-7争论如果一个图的所有边权重都是正的,那么连接所有顶点并且具有最小总权重的边的任何子集必须是一棵树。举例说明,如果我们允许某些权
假设我创建了一个 lambda 实例,稍后我想查询该对象以查看它是 proc 还是 lambda。如何做到这一点? .class() 方法不起作用。 irb(main):001:0> k = lamb
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
所以我正在将我的一个程序移植到一个新的游戏机上。问题是用于编译我的 c++ 应用程序的 SDK 不支持 __int16,但它支持 int16_t。使用 int16_t 代替 __int16 是否“安全
我最近偷看了 Url.IsLocalUrl() 方法的实现,该方法被广泛使用并被推荐为用于防止重定向攻击的措施之一。我惊讶地发现,如果我要为我的应用程序中的 Controller 的其中一个操作提供完
我读了这篇好文章 Angular onPush Change Detection Strategy 在某个时候他写道: It’s an anti-pattern to expose your subj
我是这样理解的: Innodb 中的缓冲池:它存储来自 Sql 表的索引和数据并缓存它以供将来查询,因此它不需要一直打扰 Mysql。 Memcached:用于存储从 Innodb 接收到的数据并对其
以防万一 vector 为空,(意外,异常(exception)情况) cppreference 在 vector::back() 上说: Calling back on an empty conta
我是一名优秀的程序员,十分优秀!