I have quite complex object structure (with bunch of primitive fields and object references) and want to test all fields except -a few- of them. As an example;
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"))
Since I don't want to check all fields manually, I believe there must be a way to write that test elegantly. Any ideas?
Cheers.
samePropertyValuesExceptAs
as an answer to this question? If so then you could just create a copy oforg.hamcrest.beans.SamePropertyValuesAs<T>
and add to it another constructor/static factory method that will remove the excluded properties from being tested. – Tomeassertj
: it has a more fluent api, IDE completion, and in your case, you can writeassertThat(actual).isEqualToIgnoringGivenFields(expected, "fieldName1", "fieldName2);
: joel-costigliola.github.io/assertj/index.html – Impediment