About

Jadira is the home for Sousan and Chris Pheby's open source projects. These are reusable open source Java modules that provide first class solutions using the most effective current JEE technologies.

Search
Tag Cloud
...
Login
« Comprehensive Java Artifacts Repository | Main
Monday
Jun092008

Better Assertions with Hamcrest

 James Strachan recently linked a library which is probably not so well known. This tool is so simple and useful, that, at the risk of repeating some that blog post, I'm going to talk a little about it.

 This is Hamcrest, an interesting new item for my testing toolkit, that complements well, EasyMock and JUnit 4. Hamcrest is a framework for writing matcher objects allowing 'match' rules to be defined declaratively. Effectively these can provide a  much richer set of assertions than those that come out of the box with JUnit, as well as providing assertions which are much closer to being English readable..

 The following example from the Hamcrest Tutorial give a good flavour of what Hamcrest gives you:

assertThat("hazelnuts",theBiscuit.getHazelnutCount(),equalTo(3));

 Hamcrest provides a simple framework for building new assertions, and integrates with not only JUnit, but also TestNG and other frameworks also.

 Like all good tools, Hamcrest has been published to Ibiblio. The following snippet is all you need to exploit Hamcrest from Maven:

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.1</version>
</dependency> 

PrintView Printer Friendly Version

EmailEmail Article to Friend

References (1)

References allow you to track sources for this article, as well as articles that were written in response to this article.

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>