Question

I'm currently extending Degraph to check for violation of dependency rules.

So far I have created matchers for ScalaTest, since this is my favorite test library for the JVM. But many others prefer or have to use other libraries. So I'd like to provide matchers (or whatever is the appropriate equivalent) for those as well.

On the other hand I'm lazy so I don't want to provide a matcher for every conceivable library.

So the question is:

Is there some kind of matcher library that can be used in all (or the most / many) test libraries? Or maybe a wrapper that converts Hamcrest matchers to matchers for all the other libs?

The libraries I would like to support (in that order of importance):

  • JUnit
  • ScalaTest
  • most popular testing library for Groovy
  • most popular testing library for Clojure
  • Specs2
  • TestNG
Was it helpful?

Solution

  1. Every unit testing framework I've seen will fail a test that throws an exception.
  2. Hamcrest's assertThat(T value, Matcher<T> matcher) will throw an AssertionError if matcher.matches(value) returns false.

Therefore, Hamcrest should work out-of-the-box with most testing frameworks. For other libraries, such as for building mock objects, you'll need to either use Hamcrest's Matcher objects natively or write an integration layer. One way would be to create a method to decorate any matcher, for example*,

ArgumentMatcher<T> decorate(final Matcher<T> matcher) {
    return new ArgumentMatcher() {
        public boolean <T> accepts(T value) {
            return matcher.matches(value);
        }
    }
}

* This is a contrived example using a theoretical Mocking API.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top