5

I am looking for a hamcrest matcher that behaves the same way as SamePropertyValuesAs but with the added twist that if a property is not a primitive, one of the standard Java classes (string, integer e.t.c.), or a compound of one of those (collection or array) then it will do a recursive match on those properties as well.

Is there such a matcher out there or will I have to roll my own?

iwein
  • 25,788
  • 10
  • 70
  • 111
Savvas Dalkitsis
  • 11,476
  • 16
  • 65
  • 104
  • If you did write such a Matcher, how would you identify the element that failed to match. So the 3rd object in a list A has an array B whose 15th element has a Map D whose value for key E has a array F whose third value is null when a non-null is expected. What would your failure message look like to be able to identify which field of which object failed? – John B Jan 28 '12 at 00:46
  • a[3].b[15].d(e).f[3] was null when it should not have been. – Savvas Dalkitsis Jan 28 '12 at 02:27
  • Do you really expect to be able to pass that much information down your recursion? Sounds like you will need to roll your own. If it works, post it. – John B Jan 28 '12 at 02:28
  • even if it's hard or not possible to get the actual failed match (which I don't see why not, each level of recursive call knows what field it's examining, the complete description is the stack trace of these fields), just failing with no specific info is acceptable. This is the behaviour of the existing SamePropertyValuesAs matcher anyway. It won't report the failing property – Savvas Dalkitsis Jan 28 '12 at 02:50
  • Are you sure? Seems to me that lines 99-113 of `SamePropertyValuesAs` implements the reporting of the failing property. – John B Jan 28 '12 at 11:40
  • @SavvasDalkitsis Did you write one of your own? – Ittai May 31 '13 at 05:53
  • No but thanks for reminding about this question. Take a look at my new answer – Savvas Dalkitsis Jun 02 '13 at 22:32

1 Answers1

8

The underlying issue can now be addressed by a library written by engineers at Shazam (where I work). It is a bean comparison library called Shazamcrest.

It is very powerfull and generates very nice diagnostics.

https://github.com/shazam/shazamcrest

Savvas Dalkitsis
  • 11,476
  • 16
  • 65
  • 104