Add implicit conversion for org.hamcrest.Matcher[T] to specs2.Matcher[T] #114

electricmonk opened this Issue Oct 30, 2012 · 2 comments

2 participants


In a StackOverflow question I asked, Eric showed that it's rather trivial to convert from the org.hamcrest.matcher[T] interface to Specs2's Matcher[T] trait:

I would like to see this implicit conversion (note that in the SO thread there's an error, should be Matcher[T] rather than TypeSafeMatcher[T[) added into the Specs2 codebase, either as an optional Trait or directly into the Matchers trait.

IMO backwards compatibility with Java system is a must in mature Scala frameworks. At, we have zillions of proprietary Hamcrest matchers for our domain objects, being able to use them from Scala determines if we migrate to Specs2 or not.


@etorreborre etorreborre added a commit that closed this issue Oct 30, 2012
@etorreborre fixed #114. Added a Hamcrest trait to enable implicit conversions fro…
…m Hamcrest matchers to specs2 matchers

Please try the latest 1.12.3-SNAPSHOT. Note that the reuse of some Hamcrest matchers might be problematic though due to the interoperability between Scala and Java.

For example, in the documentation I give:

// a Hamcrest matcher for even numbers
object beEven extends BaseMatcher[Int] {
  def matches(item: Object): Boolean       = item.toString.toInt % 2 == 0
  def describeTo(description: Description) { description.appendText(" is odd") }

I tried to make it a TypesafeMatcher but that didn't work because the expected type was int, the primitive type from Java, whereas the number I was passing in my test was a Scala Int.

Anyway, I hope there is now no more obstacle for you to use specs2 at :-)!



Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment