@@ -20,12 +20,12 @@ public class SamePropertyValuesAs<T> extends DiagnosingMatcher<T> {
2020 private final List <String > ignoredFields ;
2121
2222 @ SuppressWarnings ("WeakerAccess" )
23- public SamePropertyValuesAs (T expectedBean , String ... ignoredFields ) {
23+ public SamePropertyValuesAs (T expectedBean , List < String > ignoredProperties ) {
2424 PropertyDescriptor [] descriptors = propertyDescriptorsFor (expectedBean , Object .class );
2525 this .expectedBean = expectedBean ;
26- this .ignoredFields = asList ( ignoredFields ) ;
27- this .propertyNames = propertyNamesFrom (descriptors , this . ignoredFields );
28- this .propertyMatchers = propertyMatchersFor (expectedBean , descriptors , this . ignoredFields );
26+ this .ignoredFields = ignoredProperties ;
27+ this .propertyNames = propertyNamesFrom (descriptors , ignoredProperties );
28+ this .propertyMatchers = propertyMatchersFor (expectedBean , descriptors , ignoredProperties );
2929 }
3030
3131 @ Override
@@ -40,6 +40,10 @@ && hasNoExtraProperties(actual, mismatch)
4040 public void describeTo (Description description ) {
4141 description .appendText ("same property values as " + expectedBean .getClass ().getSimpleName ())
4242 .appendList (" [" , ", " , "]" , propertyMatchers );
43+ if (! ignoredFields .isEmpty ()) {
44+ description .appendText (" ignoring " )
45+ .appendValueList ("[" , ", " , "]" , ignoredFields );
46+ }
4347 }
4448
4549
@@ -75,7 +79,7 @@ private boolean hasMatchingValues(Object actual, Description mismatchDescription
7579 private static <T > List <PropertyMatcher > propertyMatchersFor (T bean , PropertyDescriptor [] descriptors , List <String > ignoredFields ) {
7680 List <PropertyMatcher > result = new ArrayList <>(descriptors .length );
7781 for (PropertyDescriptor propertyDescriptor : descriptors ) {
78- if (! ignoredFields . contains ( propertyDescriptor . getDisplayName () )) {
82+ if (isIgnored ( ignoredFields , propertyDescriptor )) {
7983 result .add (new PropertyMatcher (propertyDescriptor , bean ));
8084 }
8185 }
@@ -85,14 +89,18 @@ private static <T> List<PropertyMatcher> propertyMatchersFor(T bean, PropertyDes
8589 private static Set <String > propertyNamesFrom (PropertyDescriptor [] descriptors , List <String > ignoredFields ) {
8690 HashSet <String > result = new HashSet <>();
8791 for (PropertyDescriptor propertyDescriptor : descriptors ) {
88- final String displayName = propertyDescriptor .getDisplayName ();
89- if (! ignoredFields .contains (displayName )) {
90- result .add (displayName );
92+ if (isIgnored (ignoredFields , propertyDescriptor )) {
93+ result .add (propertyDescriptor .getDisplayName ());
9194 }
9295 }
9396 return result ;
9497 }
9598
99+ private static boolean isIgnored (List <String > ignoredFields , PropertyDescriptor propertyDescriptor ) {
100+ return ! ignoredFields .contains (propertyDescriptor .getDisplayName ());
101+ }
102+
103+ @ SuppressWarnings ("WeakerAccess" )
96104 private static class PropertyMatcher extends DiagnosingMatcher <Object > {
97105 private final Method readMethod ;
98106 private final Matcher <Object > matcher ;
@@ -132,18 +140,20 @@ private static Object readProperty(Method method, Object target) {
132140 /**
133141 * Creates a matcher that matches when the examined object has values for all of
134142 * its JavaBean properties that are equal to the corresponding values of the
135- * specified bean. If any fields are marked as ignored, they will be dropped from
136- * both the expected and actual bean.
143+ * specified bean. If any properties are marked as ignored, they will be dropped from
144+ * both the expected and actual bean. Note that the ignored properties use JavaBean
145+ * display names, for example <pre>age</pre> rather than method names such as <pre>getAge</pre>.
137146 * For example:
138147 * <pre>assertThat(myBean, samePropertyValuesAs(myExpectedBean))</pre>
139- *
148+ * <pre>assertThat(myBean, samePropertyValuesAs(myExpectedBean), "age", "height")</pre>
149+ *
140150 * @param expectedBean
141151 * the bean against which examined beans are compared
142- * @param ignoredFields
143- * do not check any of these named fields .
152+ * @param ignoredProperties
153+ * do not check any of these named properties .
144154 */
145- public static <B > Matcher <B > samePropertyValuesAs (B expectedBean , String ... ignoredFields ) {
146- return new SamePropertyValuesAs <>(expectedBean , ignoredFields );
155+ public static <B > Matcher <B > samePropertyValuesAs (B expectedBean , String ... ignoredProperties ) {
156+ return new SamePropertyValuesAs <>(expectedBean , asList ( ignoredProperties ) );
147157 }
148158
149159}
0 commit comments