liangzai-cool / hamcrest

Automatically exported from code.google.com/p/hamcrest
0 stars 0 forks source link

Enhancement request: assertThat should take boolean alone #123

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Very often, developers wish to write tests against methods that return objects, 
and those objects have their own boolean methods which the developer wishes to 
use for assertion.  For example:

User user = userService.getUser();
assertTrue(user.isAdmin());

In these cases, the boolean methods are descriptive enough to supply a very 
readable line.  

For users of hamcrest, it is difficult to take advantage of these well-named 
boolean methods while consistently using assertThat.

You can either do:

assertThat(user.isAdmin(), is(true));

or

assertThat("User is admin", user.isAdmin());

The first is needlessly verbose, and the second is redudant.

It should be possible to write

assertThat(user.isAdmin());

for simple boolean methods.

I've attached a patch that makes this addition and covers it with a test.

Original issue reported on code.google.com by rod.hil...@gmail.com on 8 Sep 2010 at 2:01

Attachments:

GoogleCodeExporter commented 9 years ago
I was thinking the same thing, though arguably, this should be added to 
org.junit.Assert, not to Hamcrest.

Original comment by MatrixFrog on 2 Jul 2011 at 1:38

GoogleCodeExporter commented 9 years ago
You should try the junit people on this one. Hamcrest is really about matching 
and reporting differences between values.

Original comment by smgfree...@gmail.com on 27 Apr 2012 at 7:07