Posts

Showing posts from June, 2009

Write More Declaritive JUnit Assertions with Hamcrest

The Hamcrest framework is a generic "matching" framework that JUnit added direct support for with version 4.4. Hamcrest allows developers to write more declarative assertions by using both built in matchers, as well as a clean way to define your own matchers. The online documentation on Hamcrest is mostly limited to some API docs, test cases, and a brief online tutorial . However the API is fairly intuitive for basic checks. One area that was intersting to me was the collection portion of the API. Hamcrest allows you to perform some assertions on collections to check for the existence of an item in the collection, but all the examples I saw were based on simple collections of items that implemented "equals" (Strings, primitives, etc). However, I wanted to find a way to validate more complex objects that may not have implemented equals. After some experimentation, I found I nice way to do what I was trying do. Basically, the collection matchers allow for embed