Posted by tomwhite
on December 22, 2006 at 12:27 PM PST
Hamcrest release 1.0 is now available. It allows you to write flexible assertions in your unit testing framework of choice.
In Literate Programming with jMock
I enthused about jMock's idea of constraints and flexible assertions .
Now the jMock team has released version 1.0 of Hamcrest ,
the constraints part of jMock.
Hamcrest matchers (what were called constraints in jMock) are actually useful for more than just writing unit tests,
but it is their application in writing assertions
where they really shine and will probably see most use.
So now I can write
rather than JUnit's
Apart from being more readable,
assertThat takes any matcher as the second argument, so
I can combine matchers or even write my own
rather than creating an ever growing listof
assertXxx methods. For example, I can say such things as
assertThat(collection, hasItem(anyOf(is(item1), is(item2))));
And since the matchers (and the
assertThat method) are accessed using static imports,
I can use them in any test framework I like.
So why not read the tutorial
and give Hamcrest a go?