JavaHamcrest icon indicating copy to clipboard operation
JavaHamcrest copied to clipboard

New BeanHas and BeanProperty matchers

Open sandromancuso opened this issue 12 years ago • 8 comments

The idea is to be able to test multiple properties in a single assertion and get a decent error message when at least one of them does not match.

I've written these matchers and we use them in a few projects at work. We thought that it would be a good idea to contribute back to JavaHamcrest. I changed my original classes to so it would fit better the coding style used in JavaHamcrest, including the way the tests are written.

I know that Hamcrest has the allOf and hasProperty matchers. They are very good but I personally think that BeanHas and BeanProperty could be a good addition to the library since they work in a slightly different way.

For more details, I've blogged about the matchers here: http://craftedsw.blogspot.co.uk/2012/05/testing-multiple-properties-with-single.html

sandromancuso avatar May 22 '12 23:05 sandromancuso

I'm curious -- what does this add that can't be done by combining multiple hasProperty into an allOf?

jonreid avatar Jun 09 '12 19:06 jonreid

I think that hasProperty works well when testing a single property. allOf is good for combining different matchers.

Using allOf and multiple hasProperty, in my view, give us a broken readability. Example:

assertThat(myObject, allOf(hasProperty(...), hasProperty(...)))

I think that assertThat(myObject(has(property(..), property(...)) gives a better readability. On top of that, BeanHas and BeanProperty are offering a better error message when the assertion fails.

sandromancuso avatar Jun 09 '12 21:06 sandromancuso

Hi guys,

Is there anyone looking at the pull requests? It would be great to have some feedback.

Cheers, Sandro

sandromancuso avatar Jun 25 '12 13:06 sandromancuso

Hey Sandro,

I'm kinda looking at pull requests, although I'm concentrating on getting a release out before taking much new code in.

scarytom avatar Jun 25 '12 18:06 scarytom

Oh god, im am looking for that @scarytom @sandromancuso

mageddo avatar May 25 '16 19:05 mageddo

I'm curious -- what does this add that can't be done by combining multiple hasProperty into an allOf?

compile time safety

rpyaset avatar Aug 28 '18 22:08 rpyaset

@sandromancuso fancy rebasing this from master, as hamcrest-core and hamcrest-library have been refactored a lot and also deprecated, so that everything is just in hamcrest.

nhojpatrick avatar Jun 28 '20 22:06 nhojpatrick

Going to try and kick start hamcrest, so if you want to get it merged, please rebase from the branch v2.3-candidates. Still trying to understand how has permissions to perform a release.

nhojpatrick avatar Feb 13 '22 12:02 nhojpatrick