|
UsesOfHamcrest
How people are using Hamcrest matchers
Window LickerWindow 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 CollectionsLambdaJ 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 JUnitAs of JUnit 4.4, Hamcrest now comes with JUnit, along with assertThat() and assumeThat() methods. assertThat(someList, containsItemWith(stringContaining("stuff"));
Mock ObjectsHamcrest 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 GetInLineGetInLine is a simple embedded Java DSL for writing applications that process record-based type files.
Behavior Driven Development with JDaveDave 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 TestingRobert 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"))));
}BabbleNat 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 IdeasThese are other uses of Hamcrest we've heard of in passing.
Doing something else with Hamcrest? Let us know! |
Sign in to add a comment
Hamcrest would be perfect for GUI tooltips telling you why something is disabled.
Is Robert Chatley's combination of Hamcrest and WebDriver? published/released anywhere? I haven't had luck finding anything related to it.
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
As far as I know, the .NET port was never implemented. We're happy for someone to contribute it though.