My favorites | Sign in
Logo
                
Search
for
Updated Aug 01, 2009 by nat.pryce
Labels: Featured, Category-Users
UsesOfHamcrest  
How people are using Hamcrest matchers

JValidation

JValidations is a framework to express and exercise validation rules for java objects. Its defining characteristics are: validation is performed by the objects themselves, not in external "validator" classes, preserving encapsulation; validation rules are expressed in declarative java using Hamcrest matchers, not in XML or annotations or what have you; how a validation failure is handled is entirely up to the caller through the use of callback interfaces; extensible in that custom validations can be coded, and the DSL syntax can be modified to suit your needs

Window Licker

Window Licker is a framework for the test-driven development of systems with Java Swing rich clients or AJAX web GUIs. It uses Hamcrest matchers to describe the expected state of the user interface.

Processing Collections

LambdaJ aims to partially eliminate the burden of writing (often nested and poorly readable) loops for iterating over collections. In particular it allows to iterate collections in order to filter items, convert items, extract a given property from each item, group or index the items on the value of one or more properties, invoke a method on each item, aggregate items or the values of one of their properties, and more! It uses reflection to provide an extremely natural way of refering to object properties.

Sam Newman's Hamcrest Collections project uses Hamcrest to implement features such as select, reject, map, reduce and zip familiar from languages like Ruby and Python.

Gareth Davis has implemented a similar collections API which also uses reflection to provide an extremely natural way of filtering by object properties:

smiths = select(people, where(Person.class).getLastName(),  equalToIgnoringCase("smith"));

Both were inspired by an Håkan Råberg's article about using Hamcrest with iterators.

Unit tests: assertThat() and assumeThat() in JUnit

As of JUnit 4.4, Hamcrest now comes with JUnit, along with assertThat() and assumeThat() methods.

assertThat(someList, containsItemWith(stringContaining("stuff"));

Mock Objects

Hamcrest was originally spawned from the Constraints code in jMock1 (which came from the Predicates code in DynaMock. The latest version of jMock (jMock2) now uses Hamcrest directly to allow for precise expectations of arguments to be specified.

// Expect one call to mockThing.doSomething(String) with a string containing "hello". 
oneOf (mockThing).doSomething(with(stringContaining("hello")));

// Expect mockThing.doSomething(String) to never be called with a null string.
never (mockThing).doSomething(with(aNull(String.class)));

The Objective-C port of Hamcrest is used by OCMock:

Mockito is a simplistic mock objects library for Java that uses Hamcrest matchers:

Record-based Text File Processing with GetInLine

GetInLine is a simple embedded Java DSL for writing applications that process record-based type files.

Behavior Driven Development with JDave

Dave is a BDD framework for Java. It is inspired by rspec and integrates jMock2 as mocking framework and Hamcrest as matching library.

Expectations are set using specify() method.

specify(persons.get(0), is(Person.class));
specify(persons, where(new Each<Person>() {{ matches(item.getAge(), is(greaterThan(30))); }}));

Web Testing

Robert Chatley has been combining Hamcrest with WebDriver in a LiFT-like way to do web-testing.

public void testHasLotsOfLinks() throws Exception {
  goTo("http://some/url");
   
  assertPresenceOf(greaterThan(15), links());
  assertPresenceOf(atLeast(1), link().with(text(equalTo("Sign in"))));

  clickOn(link().with(text(equalTo("Sign in"))));

  assertPresenceOf(exactly(1), title().with(text(equalTo("Sign in page"))));		
}

Babble

Nat Pryce has used Hamcrest to represent event subscription and advertisment filters in Babble, an event bus with subscription-based routing and dynamic federation.

session.subscribe(all(
    events(withAny(REQUEST),
           withAny(OWNER),
           with(LEASE,   greaterThanOrEqualTo(0))),
    events(withAny(RESIGN),
           withAny(OWNER),
           with(LEASE,   greaterThanOrEqualTo(0)))));

Other Ideas

These are other uses of Hamcrest we've heard of in passing.

Doing something else with Hamcrest? Let us know!


Comment by gilescope, Aug 14, 2007

Hamcrest would be perfect for GUI tooltips telling you why something is disabled.

Comment by joshua.nichols, Aug 23, 2007

Is Robert Chatley's combination of Hamcrest and WebDriver? published/released anywhere? I haven't had luck finding anything related to it.

Comment by johnzabroski, Jul 24, 2008

Whatever happened to hamcrest-dotnet? Sebastian Bergmann has a blog entry that links to a seemingly defunct hamcrest-dotnet code repository at http://fisheye3.atlassian.com/browse/hamcrest/trunk/hamcrest-dotnet

Comment by nat.pryce, Oct 13, 2008

As far as I know, the .NET port was never implemented. We're happy for someone to contribute it though.


Sign in to add a comment
Hosted by Google Code