RSS Feed for This PostCurrent Article

Using HamCrest in JUnit

HamCrest is part of JUnit starting version 4.4.

It provides a library of matcher objects (also known as constraints or predicates) allowing ‘match’ rules to be defined declaratively, to be used in other frameworks. Typical scenarios include testing frameworks, mocking libraries and UI validation rules.

Using HamCrest, in JUnit you can now write assertions like the followings

   1: String result = "red";
   2: ...
   3: assertThat(result, equalTo("red"));
   5: assertThat(color, anyOf(is("red"),is("green"),is("yellow")));

Hamcrest has been designed from the outset to integrate with different unit testing frameworks like JUnit 3 and 4 and TestNG.

It can also be used with mock objects frameworks by using adaptors to bridge from the mock objects framework’s concept of a matcher to a Hamcrest matcher.

Popularity: 1% [?]

Trackback URL

RSS Feed for This PostPost a Comment