我有相当复杂的对象结构(带有一堆原始字段和对象引用),并且想要测试除-a部分之外的所有字段。举个例子;Hamcrest - 优雅的方式来测试具有相同propertyity值的复杂对象
ComplexObject actual = generateMagically("someInput");
ComplexObject expected = ActualFunction.instance.workMagically(actual);
// we want to be sure that workMagically() would create a new ComplexObject
// with some fields are different than "actual" object.
// assertThat(actual, samePropertyValuesAs(expected)); would check all fields.
// what I want is actually; - notice that "fieldName1" and "fieldName2" are
// primitives belong to ComplexObject
assertThat(actual, samePropertyValuesExceptAs(expected, "fieldName1", "fieldName2"))
由于我不想手动检查所有字段,我相信必须有一种方法来优雅地编写测试。有任何想法吗?
干杯。
所以,你有两个袋* *的对象,想进行深度比较呢? – Raffaele
不确定要叫他们包对象,有许多原始字段和其他对象引用。我想跳过的字段是属于ComplexObject的原始字段。我们可以说这将是一个深刻的比较。 – tugcem
你是否只想''samePropertyValuesExceptAs'的Matcher实现作为这个问题的答案?如果是这样的话,你可以创建'org.hamcrest.beans.SamePropertyValuesAs'的副本,并添加另一个构造函数/静态工厂方法,该方法将删除被排除的属性进行测试。 –
SpaceTrucker