|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.hamcrest.BaseMatcher<T>
org.hamcrest.DiagnosingMatcher<T>
com.atlassian.hamcrest.DeepIsEqual<T>
public class DeepIsEqual<T>
A matcher that does a deep equals comparison of objects using reflection. This allows testers to compare arbitrary
objects, whether they implement the Object.equals(java.lang.Object) method or not. It only tries to match 2 objects of the
exact same type. If one type is a sub-type of another, the match will fail. The actual and expected objects are
compared by reflectively finding the types' declared, non-static, non-transient fields and the declared, non-static,
non-transient fields of its parents. There is a core set of matchers used to compare the values of primitive field
types and arrays. If the field is an array, then each element of the array is compared.
A tester can also provide a way to create custom Matchers for field types. To provide custom matchers
for certain field types, you pass in a Map that indicates the type of the field you want to use a custom
matcher for and how to create the custom matcher. Hamcrest Matchers are used to indicate the field type,
so you can use equalTo to match types exactly or create your own to match subclasses, etc. Once a matching
type is found, a Matcher is created using the MatcherFactory.
As an example of how this works, you can register a MatcherFactory which will be used when the field type
is a java.security.Key. For this example, we really want a type matcher that checks if the type of the value
implements the Key interface, because it will never match exactly. To do that, we'll use the
ClassMatchers.isAssignableTo(Class) matcher and we'll say we want keys to match exactly using the in-built
MatcherFactories.isEqual() MatcherFactory.
Map>, MatcherFactory> matcherFactories = new HashMap>, MatcherFactory>();
matcherFactories.put(isAssignableTo(Key.class), isEqual());
assertThat(actualValue, is(deeplyEqualTo(expectedValue, matcherFactories)));
Because that isn't exactly the clearest code, it is highly recommended that you create utility methods that
encapsulate the creation of the the MatcherFactory maps and registration of them to make your tests easier
to read.
Note: There is currently no support for cyclic object graphs and no effort made to detect them. If there is a
cycle in your object graph it will cause a StackOverflowError. If you have cycles in your object graph you
can overcome this limitation by providing a custom matcher.
| Method Summary | ||
|---|---|---|
static
|
deeplyEqualTo(T operand)
Returns a Matcher which compares two objects reflectively. |
|
static
|
deeplyEqualTo(T operand,
java.util.Map<org.hamcrest.Matcher<java.lang.Class<?>>,MatcherFactory> extraMatcherFactories)
Returns a Matcher which compares 2 objects reflectively and uses the custom MatcherFactorys to
determine how to match certain types of fields. |
|
void |
describeTo(org.hamcrest.Description description)
|
|
protected boolean |
matches(java.lang.Object actual,
org.hamcrest.Description mismatchDescription)
Checks that the type of actual matches the type of the expected value and then that the composite object
values are equal. |
|
| Methods inherited from class org.hamcrest.DiagnosingMatcher |
|---|
describeMismatch, matches |
| Methods inherited from class org.hamcrest.BaseMatcher |
|---|
_dont_implement_Matcher___instead_extend_BaseMatcher_, toString |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Method Detail |
|---|
protected boolean matches(java.lang.Object actual,
org.hamcrest.Description mismatchDescription)
actual matches the type of the expected value and then that the composite object
values are equal.
matches in class org.hamcrest.DiagnosingMatcher<T>public void describeTo(org.hamcrest.Description description)
public static <T> org.hamcrest.Matcher<? super T> deeplyEqualTo(T operand)
Matcher which compares two objects reflectively.
T - type of the objects to compareoperand - the expected value
Matcher which compares two objects reflectively
public static <T> org.hamcrest.Matcher<? super T> deeplyEqualTo(T operand,
java.util.Map<org.hamcrest.Matcher<java.lang.Class<?>>,MatcherFactory> extraMatcherFactories)
Matcher which compares 2 objects reflectively and uses the custom MatcherFactorys to
determine how to match certain types of fields.
T - type of the objects to compareoperand - the expected valueextraMatcherFactories - MatcherFactorys to use for the fields with types matching the key Matcher
Matcher which compares 2 objects reflectivelyDeepIsEqual
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||