matchers icon indicating copy to clipboard operation
matchers copied to clipboard

FieldMatcher is unable to compare private field values

Open ZeroOne3010 opened this issue 5 years ago • 1 comments

The FieldMatcher, which is supposed to work like hasField(String fieldName, Matcher<?> valueMatcher) when comparing field values, is unable to do the comparing if the field is private: it internally gets an exception like java.lang.IllegalAccessException: Class com.nitorcreations.matchers.FieldMatcher can not access a member of class foo.bar.Baz with modifiers "private".

The FieldMatcher is able to find the private fields and assert their existence just fine when used without the value matcher. However, I need to assert the value too. Alternatively, I would need a way to assert the return value of the getter of the private field.

My use case is that I've got a collection of entities where only two fields are interesting and the rest should be ignored:

assertThat(all, containsInAnyOrder(
    allOf(hasField("foo", equalTo("1111")), hasField("bar", equalTo("444"))),
    allOf(hasField("foo", equalTo("2222")), hasField("bar", equalTo("555"))),
    allOf(hasField("foo", equalTo("3333")), hasField("bar", equalTo("666")))
));

ZeroOne3010 avatar Nov 28 '18 08:11 ZeroOne3010