- Every unit testing framework I've seen will fail a test that throws an exception.
- Hamcrest's
assertThat(T value, Matcher<T> matcher)
will throw anAssertionError
ifmatcher.matches(value)
returnsfalse
.
Therefore, Hamcrest should work out-of-the-box with most testing frameworks. For other libraries, such as for building mock objects, you'll need to either use Hamcrest's Matcher
objects natively or write an integration layer. One way would be to create a method to decorate any matcher, for example*,
ArgumentMatcher<T> decorate(final Matcher<T> matcher) {
return new ArgumentMatcher() {
public boolean <T> accepts(T value) {
return matcher.matches(value);
}
}
}
* This is a contrived example using a theoretical Mocking API.