JUnit 4.7 and Hamcrest 1.2

David Saff <[email protected]> Fri, 29 May 2009 12:44:22 -0400
Newsgroups gmane.comp.java.junit.devel
Message-ID <[email protected]>
All,

I've spent an amazing chunk of this week getting Hamcrest 1.2 and
JUnit 4.7 to compile and play nicely together under both Eclipse
(easy) and javac 1.5 (hard).  In the end, I was thrilled to get
hasItem, hasItems, each, and containsString out of the JUnitMatchers
factory class, but I had to leave four convenience functions:

*** JUnitMatchers.both: Hamcrest 1.2 included both(), but changed its
type signature to allow this to compile:

CoreMatchers.both(is(3)).and(is(4))

This is a fairly rare thing to truly want to say.  This is more
common, and now does not compile:

CoreMatchers.both(containsString("first")).and(containsString("second"))

So, I left both() in JUnitMatchers with the original typing.  I'd be
interested in if Hamcrest would like to make the same choice.

*** JUnitMatchers.either: same as above, although with less certainty,
since this is more likely to be useful:

CoreMatchers.either(is(3)).or(is(4))

Thus...

*** JUnitMatchers.isOneOf(T...)

Instead of either(is(3)).or(is(4)), use

JUnitMatchers.isOneOf(3, 4);

*** JUnitMatchers.matches

Takes any matcher and returns a matcher of any other type:

	@SuppressWarnings("unchecked")
	public static <T> Matcher<T> matches(Matcher<?> matcher) {
		return (Matcher<T>)matcher;
	}

I expect to be saying "just wrap it in matches()" any number of times
on the JUnit mailing lists coming up.

   David Saff

------------------------------------------------------------------------------
Register Now for Creativity and Technology (CaT), June 3rd, NYC. CaT 
is a gathering of tech-side developers & brand creativity professionals. Meet
the minds behind Google Creative Lab, Visual Complexity, Processing, & 
iPhoneDevCamp as they present alongside digital heavyweights like Barbarian 
Group, R/GA, & Big Spaceship. http://p.sf.net/sfu/creativitycat-com