Package org.hamcrest.beans
Class SamePropertyValuesAs<T>
- java.lang.Object
-
- org.hamcrest.BaseMatcher<T>
-
- org.hamcrest.DiagnosingMatcher<T>
-
- org.hamcrest.beans.SamePropertyValuesAs<T>
-
- All Implemented Interfaces:
Matcher<T>
,SelfDescribing
public class SamePropertyValuesAs<T> extends DiagnosingMatcher<T>
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
SamePropertyValuesAs.PropertyMatcher
-
Field Summary
Fields Modifier and Type Field Description private T
expectedBean
private java.util.List<java.lang.String>
ignoredFields
private java.util.List<SamePropertyValuesAs.PropertyMatcher>
propertyMatchers
private java.util.Set<java.lang.String>
propertyNames
-
Constructor Summary
Constructors Constructor Description SamePropertyValuesAs(T expectedBean, java.util.List<java.lang.String> ignoredProperties)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
describeTo(Description description)
Generates a description of the object.private boolean
hasMatchingValues(java.lang.Object actual, Description mismatchDescription)
private boolean
hasNoExtraProperties(java.lang.Object actual, Description mismatchDescription)
private boolean
isCompatibleType(java.lang.Object actual, Description mismatchDescription)
private static boolean
isIgnored(java.util.List<java.lang.String> ignoredFields, java.beans.PropertyDescriptor propertyDescriptor)
protected boolean
matches(java.lang.Object actual, Description mismatch)
private static <T> java.util.List<SamePropertyValuesAs.PropertyMatcher>
propertyMatchersFor(T bean, java.beans.PropertyDescriptor[] descriptors, java.util.List<java.lang.String> ignoredFields)
private static java.util.Set<java.lang.String>
propertyNamesFrom(java.beans.PropertyDescriptor[] descriptors, java.util.List<java.lang.String> ignoredFields)
private static java.lang.Object
readProperty(java.lang.reflect.Method method, java.lang.Object target)
static <B> Matcher<B>
samePropertyValuesAs(B expectedBean, java.lang.String... ignoredProperties)
Creates a matcher that matches when the examined object has values for all of its JavaBean properties that are equal to the corresponding values of the specified bean.-
Methods inherited from class org.hamcrest.DiagnosingMatcher
describeMismatch, matches
-
Methods inherited from class org.hamcrest.BaseMatcher
_dont_implement_Matcher___instead_extend_BaseMatcher_, isNotNull, toString
-
-
-
-
Field Detail
-
expectedBean
private final T expectedBean
-
propertyNames
private final java.util.Set<java.lang.String> propertyNames
-
propertyMatchers
private final java.util.List<SamePropertyValuesAs.PropertyMatcher> propertyMatchers
-
ignoredFields
private final java.util.List<java.lang.String> ignoredFields
-
-
Constructor Detail
-
SamePropertyValuesAs
public SamePropertyValuesAs(T expectedBean, java.util.List<java.lang.String> ignoredProperties)
-
-
Method Detail
-
matches
protected boolean matches(java.lang.Object actual, Description mismatch)
- Specified by:
matches
in classDiagnosingMatcher<T>
-
describeTo
public void describeTo(Description description)
Description copied from interface:SelfDescribing
Generates a description of the object. The description may be part of a a description of a larger object of which this is just a component, so it should be worded appropriately.- Parameters:
description
- The description to be built or appended to.
-
isCompatibleType
private boolean isCompatibleType(java.lang.Object actual, Description mismatchDescription)
-
hasNoExtraProperties
private boolean hasNoExtraProperties(java.lang.Object actual, Description mismatchDescription)
-
hasMatchingValues
private boolean hasMatchingValues(java.lang.Object actual, Description mismatchDescription)
-
propertyMatchersFor
private static <T> java.util.List<SamePropertyValuesAs.PropertyMatcher> propertyMatchersFor(T bean, java.beans.PropertyDescriptor[] descriptors, java.util.List<java.lang.String> ignoredFields)
-
propertyNamesFrom
private static java.util.Set<java.lang.String> propertyNamesFrom(java.beans.PropertyDescriptor[] descriptors, java.util.List<java.lang.String> ignoredFields)
-
isIgnored
private static boolean isIgnored(java.util.List<java.lang.String> ignoredFields, java.beans.PropertyDescriptor propertyDescriptor)
-
readProperty
private static java.lang.Object readProperty(java.lang.reflect.Method method, java.lang.Object target)
-
samePropertyValuesAs
public static <B> Matcher<B> samePropertyValuesAs(B expectedBean, java.lang.String... ignoredProperties)
Creates a matcher that matches when the examined object has values for all of its JavaBean properties that are equal to the corresponding values of the specified bean. If any properties are marked as ignored, they will be dropped from both the expected and actual bean. Note that the ignored properties use JavaBean display names, for exampleage
rather than method names such asgetAge
. For example:assertThat(myBean, samePropertyValuesAs(myExpectedBean))
assertThat(myBean, samePropertyValuesAs(myExpectedBean), "age", "height")
- Parameters:
expectedBean
- the bean against which examined beans are comparedignoredProperties
- do not check any of these named properties.
-
-