如何使用assertJ比较递归地忽略给定的字段? [英] How to compare recursively ignoring given fields using assertJ?

查看:139
本文介绍了如何使用assertJ比较递归地忽略给定的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AssertJ具有isEqualToIgnoringGivenFieldsisEqualToComparingFieldByFieldRecursively.

但是,我无法通过忽略某些字段来递归比较两个对象.根据讨论,它必须在开发中.

But, there is no way I can compare two objects recursively by ignoring some fields. As per this discussion, it must be in development.

如何仍然使我的断言的返回值进行递归比较,但忽略某些字段.可以在任何其他库中使用还是可以使用AssertJ做到这一点?

How to still get my assert's return value to be compared recursively but ignoring some fields. Is it possible in any other library or can I do it somehow using AssertJ?

推荐答案

我无法让它忽略某些字段,但通过为要忽略的字段引入比较器并始终对其进行评估,设法设法暂时解决了该问题真实.这不是流利的,而是完成工作的临时解决方案.

I couldn't get it to ignore some fields but I managed to temporarily solve it by introducing a comparator for the fields which I want to ignore and always evaluated them to true. This is not fluent but a temporary solution to get the job done.

assertThat(object).usingComparatorForFields((x,y)->0,"field1","field2").isEqualToComparingFieldByFieldRecursively(expectedObject);

自2017年4月13日起,这具有问题.<一个href ="https://stackoverflow.com/questions/43370317/how-does-assertjs-usingcomparatorforfields-work?noredirect=1&lq=1">它不起作用正在尝试比较的结果为null.如果其中一个对象为null而不是两个都为null,则这是一个问题.

This has an issue as of April 13, 2017. It doesn't work when the fields which the comparator is trying to compare are null. This is an issue when one of objects is null not if both are null.

这篇关于如何使用assertJ比较递归地忽略给定的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆